Created
April 14, 2013 00:52
-
-
Save jackc/5380844 to your computer and use it in GitHub Desktop.
Test to show how slices automatically grow their capacity
This file contains hidden or 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 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