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.

examples/array-pointer/array_pointer.go
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)
}

examples/array-pointer/array_pointer.out
[Foo Bar]
[Foo Bar]
&[Foo Bar]

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