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
+46.6k Golang : Marshal and unmarshal json.RawMessage struct example
+7.1k Golang : Takes a plural word and makes it singular
+16.3k Golang : Loop each day of the current month example
+9.1k Golang : How to use Gorilla webtoolkit context package properly
+15.3k nginx: [emerg] unknown directive "ssl"
+20.9k Golang : Underscore or snake_case to camel case example
+15.5k Golang : Find location by IP address and display with Google Map
+18.6k Golang : Set, Get and List environment variables
+20.2k Golang : Reset or rewind io.Reader or io.Writer
+5.5k Golang : Display advertisement images or strings on random order
+6.2k Linux/Unix : Commands that you need to be careful about
+6.8k Golang : Output or print out JSON stream/encoded data