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
+26.8k PHP : Convert(cast) string to bigInt
+11.6k Golang : How to parse plain email text and process email header?
+8.7k nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
+21.4k Golang : How to reverse slice or array elements order
+14.8k Golang : Get query string value on a POST request
+5.6k Fix ERROR 2003 (HY000): Can't connect to MySQL server on 'IP address' (111)
+9.1k Golang : Convert(cast) string to int64
+10.1k Golang : How to unmarshal JSON inner/nested value and assign to specific struct?
+6.7k Javascript : How to get JSON data from another website with JQuery or Ajax ?
+8.8k Golang : How to check if a string with spaces in between is numeric?
+5k Golang : Reclaim memory occupied by make() example