31.Pointers to structs - Programming in GO

Posted on Apr 02, 2019   ∣  1 min read  ∣  GO

Struct fields can be accessed through a struct pointer.

To access the field Xof a struct when we have the struct pointer pwe could write(*p).X. However, that notation is cumbersome, so the language permits us instead to write just p.X, without the explicit dereference.

func main() {
	z := xy{1, 2}
	p := &z
	p.X = 20
	fmt.Println(z)
}

Go Program

package main

import "fmt"

type xy struct {
	X int
	Y int
}

func main() {
	z := xy{1, 2}
	p := &z
	p.X = 20
	fmt.Println(z)
}