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
+6.8k Golang : Decode XML data from RSS feed
+20.5k Golang : Read directory content with os.Open
+7.5k Golang : Command line ticker to show work in progress
+6.1k Javascript : Generate random key with specific length
+15.9k Golang : Get file permission
+5.2k Golang : Pad file extension automagically
+7k Golang : Gorrila mux.Vars() function example
+12.1k Golang : List running EC2 instances and descriptions
+5.2k JavaScript/JQuery : Redirect page examples
+6.7k Golang : Pat multiplexer routing example
+15.1k Golang : Get HTTP protocol version example
+9.1k Golang : Create and shuffle deck of cards example