Skip to content

Instantly share code, notes, and snippets.

@tjs-w
Last active February 19, 2016 00:19
Show Gist options
  • Save tjs-w/3bc86dff9cca9c6ce535 to your computer and use it in GitHub Desktop.
Save tjs-w/3bc86dff9cca9c6ce535 to your computer and use it in GitHub Desktop.
Golang Slice Behavior Example
package main
import "fmt"
func printSlice(s string, x []int) {
fmt.Printf("Slice %s len=%d cap=%d %v\n", s, len(x), cap(x), x)
}
func main() {
fmt.Println("Go Slice Example: Copy-on-Capacity-Overload\n")
a := make([]int, 7)
b := a[2:5]
printSlice("a", a)
fmt.Println("Slice b referes to the 2, 3, 4 indices in slice a. Hence, the capcity is 5 (= 7-2).")
fmt.Println("b := a[2:5]")
printSlice("b", b)
fmt.Println("\nModifying slice b, also modifies a, since they are pointing to the same underlying array.")
fmt.Println("b[0] = 9")
b[0] = 9
printSlice("a", a)
printSlice("b", b)
fmt.Println("\nAppending 1 to slice b. Overwrites a.")
b = append(b, 1)
printSlice("a", a)
printSlice("b", b)
fmt.Println("\nAppending 2 to slice b. Overwrites a.")
b = append(b, 2)
printSlice("a", a)
printSlice("b", b)
fmt.Println("\nAppending 3 to slice b. Here, a new copy is made as the capacity is overloaded.")
b = append(b, 3)
printSlice("a", a)
printSlice("b", b)
fmt.Println("\nVerifying slices a and b point to different underlying arrays after the capacity-overload in the previous step.")
fmt.Println("b[1] = 8")
b[1] = 8
printSlice("a", a)
printSlice("b", b)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment