In this video, we are going to be looking at pointers in Go and what they are used for.
Code
package main
import "fmt"
// Engineer - stores the name and age of an engineer
type Engineer struct {
Name string
Age int
}
func (e *Engineer) UpdateAge() {
e.Age += 1
}
func (e *Engineer) UpdateName() {
e.Name = "new name"
fmt.Println(e)
}
func UpdateAge(e *Engineer) {
e.Age += 1
}
func main() {
fmt.Println("Go Pointers Tutorial")
elliot := &Engineer{
Name: "Elliot",
Age: 27,
}
fmt.Println(elliot)
elliot.UpdateAge()
fmt.Println(elliot)
elliot.UpdateName()
fmt.Println(elliot)
UpdateAge(elliot)
fmt.Println(elliot)
}
Output
$ go run main.go
Go Pointers Tutorial
&{Elliot 27}
&{Elliot 28}
&{new name 28}
&{new name 28}
&{new name 29}