Skip to content

Instantly share code, notes, and snippets.

@rdarder
Created February 6, 2012 19:11
Show Gist options
  • Save rdarder/1754148 to your computer and use it in GitHub Desktop.
Save rdarder/1754148 to your computer and use it in GitHub Desktop.
array access references and copies
package main
import "fmt"
type foo struct {
val int
}
func main(){
arr := make([]foo, 5)
for i:=0; i< len(arr); i++ {
arr[i] = foo{1}
}
fmt.Printf("%v\n", arr)
ref := arr[0]
ref.val = 2
arr[1].val = 3
ref2 := &(arr[2])
ref2.val = 4
fmt.Printf("%v\n", arr)
}
//output
//[{1} {1} {1} {1} {1}]
//[{1} {3} {4} {1} {1}]
~
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment