Created
January 26, 2014 02:44
-
-
Save wweic/8627521 to your computer and use it in GitHub Desktop.
weird-go.go
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" | |
) | |
func a ()[]int { | |
var arr [16]int | |
for i := range arr { | |
arr[i] = i | |
} | |
return arr[:] | |
} | |
func b (arr []int) { | |
for i := range arr { | |
arr[i] = -i | |
} | |
} | |
// this example shows slice is actually a referece, the underlying is invisiable in many cases. | |
// we need to understand this in case use stale or corrupted data. | |
func main() { | |
arr := a () | |
fmt.Println(arr) | |
// modify the underlying array | |
b (arr) | |
fmt.Println(arr) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment