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
+22.2k Golang : Print out struct values in string format
+9.5k Swift : Convert (cast) String to Integer
+19.8k Golang : Sort and reverse sort a slice of strings
+11.3k Golang : Sort and reverse sort a slice of bytes
+11.8k Golang : Qt progress dialog example
+3.6k Linux/MacOSX : Search and delete files by extension
+8.6k Golang : Populate slice with sequential integers example
+6.5k Golang : Command line ticker to show work in progress
+16.3k Golang : Login and logout a user after password verification and redirect example
+9.6k Golang : Get currencies exchange rates example
+12.4k Golang : Query string with space symbol %20 in between