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
+7.5k Golang : File system scanning
+16.9k Golang : Get own process identifier
+4.8k JavaScript : Rounding number to decimal formats to display currency
+17.3k Golang : XML to JSON example
+7k Golang : Muxing with Martini example
+6.4k Golang : Process non-XML/JSON formatted ASCII text file example
+6k Golang : Detect variable or constant type
+12.5k Golang : Validate email address
+6.4k Golang & Javascript : How to save cropped image to file on server
+8.3k Golang : Randomize letters from a string example
+9.4k Golang : How to check if a string with spaces in between is numeric?
+6.1k Golang : Shuffle array of list