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.6k Golang : Bcrypting password
+14.9k Golang : Get file permission
+6.8k Android Studio : Rating bar example
+6.1k Golang : Gorrila mux.Vars() function example
+12.7k Golang: Pad right or print ending(suffix) zero or spaces in fmt.Printf example
+5.3k Golang : Break string into a slice of characters example
+6.8k Golang : Configure Apache and NGINX to access your Go service example
+9.8k Golang : Read until certain character to break for loop
+19.5k Golang : Create and resolve(read) symbolic links
+4.6k Unix/Linux/MacOSx : Get local IP address
+5.2k Golang : Handling image beyond OpenCV video capture boundary