Skip to content

Instantly share code, notes, and snippets.

@wweic
Created January 26, 2014 02:44
Show Gist options
  • Save wweic/8627521 to your computer and use it in GitHub Desktop.
Save wweic/8627521 to your computer and use it in GitHub Desktop.
weird-go.go
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