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
+5.3k Golang : The Tao of importing package
+21.4k Golang : Create and resolve(read) symbolic links
+6.8k Golang : Output or print out JSON stream/encoded data
+9.1k Golang : Go as a script or running go with shebang/hashbang style
+19.9k Golang : Accept input from user with fmt.Scanf skipped white spaces and how to fix it
+21.2k Golang : For loop continue,break and range
+25.5k Golang : Convert long hexadecimal with strconv.ParseUint example
+10.5k Golang : Meaning of omitempty in struct's field tag
+3.8k Golang : Switch Redis database redis.NewClient
+9k Golang : Inject/embed Javascript before sending out to browser example
+17.8k Golang : Read data from config file and assign to variables
+4.8k Facebook : How to place save to Facebook button on your website