25% off

Use code FUNCMAIN at checkout for 25% off all premium courses.

Get started →

Pointers in Go

June 24, 2021
Elliot Forbes

Elliot Forbes

Course Instructor

Hey Gophers! My name is Elliot and I'm the creator of TutorialEdge and I've been working with Go systems for roughly 5 years now.

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}