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
+9.2k Golang : Find network of an IP address
+14k Swift : Convert (cast) Int to int32 or Uint32
+12.3k Golang : Get future or past hours, minutes or seconds
+2.3k Detect if Google Analytics and Developer Media are loaded properly or not
+7.6k Golang : Bubble sort example
+12.1k Golang : Measure http.Get() execution time
+9.2k Javascript : Prompt confirmation before exit
+28.5k Delete a directory in Go
+3.5k Golang : Selection sort example
+5.6k nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
+4.9k Golang : Intercept and compare HTTP response code example
+6.7k Golang : Get remaining text such as id or filename after last segment in URL path