Dereferencing pointers

suggest change

Dereference a pointer by placing the * operator before it.

type Person struct {
	Name string
}

func main() {
	c := new(Person) // returns pointer
	c.Name = "Catherine"
	fmt.Println(c.Name) // prints: Catherine
	d := c
	d.Name = "Daniel"
	fmt.Println(c.Name) // prints: Daniel
	// Dereferencing d copies the pointed-to Person value into i.
	i := *d
	i.Name = "Ines"
	fmt.Println(c.Name) // prints: Daniel
	fmt.Println(d.Name) // prints: Daniel
	fmt.Println(i.Name) // prints: Ines
Catherine
Daniel
Daniel
Daniel
Ines

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents