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
+11.3k Golang : How to determine a prime number?
+7.5k Golang : Word limiter example
+8k Golang : Gomobile init produce "iphoneos" cannot be located error
+27.8k Golang : dial tcp: too many colons in address
+16.5k Golang : Find out mime type from bytes in buffer
+5.5k Golang : Display advertisement images or strings on random order
+33.1k Delete a directory in Go
+11k Android Studio : Checkbox for user to select options example
+17.7k Golang : Clone with pointer and modify value
+26.4k Golang : Calculate future date with time.Add() function
+15.5k Golang : Get all local users and print out their home directory, description and group id
+7.2k Javascript : How to get JSON data from another website with JQuery or Ajax ?