Created
November 20, 2023 17:44
-
-
Save yarysh/2669426e755e2d83059438aa08d8e19b to your computer and use it in GitHub Desktop.
How do slices grow in Go
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() { | |
| initLen, insertions := 64, 1024*10 | |
| s := make([]int, 0, initLen) | |
| currCap := cap(s) | |
| for i := 0; i < insertions; i++ { | |
| s = append(s, i) | |
| if currCap < cap(s) { | |
| fmt.Printf( | |
| "Capacity was increased from %d to %d (%.2f%%)\n", | |
| currCap, cap(s), (1-float64(currCap)/float64(cap(s)))*100, | |
| ) | |
| currCap = cap(s) | |
| } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
go run main.go