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
+8.9k Golang : Find network service name from given port and protocol
+7.9k Golang : Lock executable to a specific machine with unique hash of the machine
+19.5k Golang : Fix cannot download, $GOPATH not set error
+55.4k Golang : Unmarshal JSON from http response
+11.7k Golang : Simple file scaning and remove virus example
+19.6k Golang : Example for DSA(Digital Signature Algorithm) package functions
+14.2k Golang : Check if a file exist or not
+12.2k Golang : md5 hash of a string
+12.6k Golang : "https://" not allowed in import path
+8.4k Golang : Auto-generate reply email with text/template package
+25.2k Golang : Create PDF file from HTML file
+18.4k Golang : Read binary file into memory