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
+15.4k Golang : Get current time from the Internet time server(ntp) example
+7.4k Golang : Command line ticker to show work in progress
+20.3k Golang : Secure(TLS) connection between server and client
+17.1k Golang : Check if IP address is version 4 or 6
+12.2k Elastic Search : Return all records (higher than default 10)
+8.4k Golang : Progress bar with ∎ character
+7.2k Golang : Rot13 and Rot5 algorithms example
+6.8k Golang : constant 20013 overflows byte error message
+14.5k Golang : Adding XML attributes to xml data or use attribute to differentiate a common tag name
+7.3k Golang : Handling Yes No Quit query input
+11.4k Golang : Calculations using complex numbers example
+9.4k Golang : Populate slice with sequential integers example