Files

38 lines
824 B
Go
Raw Permalink Normal View History

2019-03-04 20:30:18 +03:00
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
2019-10-30 19:34:44 +03:00
// For more tutorials : https://learngoprogramming.com
// In-person training : https://www.linkedin.com/in/inancgumus/
// Follow me on twitter: https://twitter.com/inancgumus
2019-03-04 20:30:18 +03:00
package main
import "fmt"
func main() {
var (
nums []int
oldCap float64
)
2019-08-18 15:08:32 +03:00
// loop 10 million times
2021-05-01 13:21:56 +03:00
for len(nums) < 1e7 {
2019-08-18 15:08:32 +03:00
// get the capacity
2019-03-04 20:30:18 +03:00
c := float64(cap(nums))
2019-08-18 15:08:32 +03:00
// only print when the capacity changes
2019-03-04 20:30:18 +03:00
if c == 0 || c != oldCap {
2019-08-18 15:08:32 +03:00
// print also the growth ratio: c/oldCap
2019-03-04 20:30:18 +03:00
fmt.Printf("len:%-15d cap:%-15g growth:%-15.2f\n",
len(nums), c, c/oldCap)
}
2019-08-18 15:08:32 +03:00
// keep track of the previous capacity
2019-03-04 20:30:18 +03:00
oldCap = c
2019-08-18 15:08:32 +03:00
// append an arbitrary element to the slice
2019-03-04 20:30:18 +03:00
nums = append(nums, 1)
}
}