Skip to content

Instantly share code, notes, and snippets.

@jackc
Created April 14, 2013 00:52
Show Gist options
  • Save jackc/5380844 to your computer and use it in GitHub Desktop.
Save jackc/5380844 to your computer and use it in GitHub Desktop.
Test to show how slices automatically grow their capacity
package main
import "fmt"
func main() {
int64Slice := make([]int64, 0)
lastCap := -1
for i := 0; i < 65536; i++ {
currentCap := cap(int64Slice)
if currentCap != lastCap {
fmt.Printf("[]int64 -- len: %d, cap: %d\n", len(int64Slice), currentCap)
lastCap = currentCap
}
int64Slice = append(int64Slice, 0)
}
int16Slice := make([]int16, 0)
lastCap = -1
for i := 0; i < 65536; i++ {
currentCap := cap(int16Slice)
if currentCap != lastCap {
fmt.Printf("[]int16 -- len: %d, cap: %d\n", len(int16Slice), currentCap)
lastCap = currentCap
}
int16Slice = append(int16Slice, 0)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment