Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Array Pointer

Just as with variables holding integers, variables holding arrays are also copied dirng assignment. Here too you can use a pointer to have two ways to access the same array.

  • Assigning an array creates a copy of the data.
  • One can assign a pointer and then both variables access the same place in memory.
package main

import (
	"fmt"
)

func main() {
	a := [...]string{"Foo", "Bar"}
	b := a
	c := &a
	fmt.Println(a)
	fmt.Println(b)
	fmt.Println(c)
	fmt.Println()

	a[0] = "Zorg"
	fmt.Println(a)
	fmt.Println(b)
	fmt.Println(c)
}
[Foo Bar]
[Foo Bar]
&[Foo Bar]

[Zorg Bar]
[Foo Bar]
&[Zorg Bar]