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
+4.8k PHP : Get client IP address
+14.9k Golang : Read integer from file into array
+16.1k Golang : How to make a file read only and set it to writable again?
+13.7k Golang : Get HTTP protocol version example
+5.3k Golang : Extract sub-strings
+7.7k Golang : Accept any number of function arguments with three dots(...)
+12k Golang : Get user input until a command or receive a word to stop
+8.1k Golang : Play .WAV file from command line
+29k Golang : Calculate percentage change of two values
+11.2k Golang : Search and extract certain XML data example
+8.6k Golang : Validate IPv6 example
+10k Golang : Concatenate (combine) buffer data example