Golang : How to get capacity of a slice or array?
Problem :
You have a slice and you need to find out the capacity of the slice.
Solution :
Use the builtin function cap()
to determine the slice's maximum length or the number of elements inside the array.
For example :
package main
import (
"fmt"
)
var a = make([]int, 7, 8)
func main() {
for i := 0; i < 7; i++ {
a[i] = i
}
fmt.Println(a)
fmt.Printf("Size of slice a is : %d", cap(a))
}
References :
See also : Golang : Delete duplicate items from a slice/array
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+3.5k Golang : Calculate half life decay example
+7.3k Golang : Populate or initialize struct with values example
+17k Golang : Convert PNG transparent background image to JPG or JPEG image
+9.8k Golang : Convert(cast) float to int
+22.1k Golang : Call function from another package
+6k Golang : How to find out similarity between two strings with Jaro-Winkler Distance?
+10.6k Golang : Set image canvas or background to transparent
+4.4k Golang : Map within a map example
+7.8k Golang : Bcrypting password
+4.1k Unix/Linux : How to open tar.gz file ?
+9.4k Get form post value in Go
+15k Golang : [json: cannot unmarshal object into Go value of type]