Created
February 6, 2012 19:11
-
-
Save rdarder/1754148 to your computer and use it in GitHub Desktop.
array access references and copies
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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