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
+12.1k Golang : convert(cast) string to integer value
+17.7k Golang : [json: cannot unmarshal object into Go value of type]
+12.7k Golang : Pass database connection to function called from another package and HTTP Handler
+8.3k Golang : Count leading or ending zeros(any item of interest) example
+6.9k Mac OSX : Find large files by size
+9.7k PHP : Get coordinates latitude/longitude from string
+7.8k Golang : Scan files for certain pattern and rename part of the files
+19.9k Golang : Count JSON objects and convert to slice/array
+6.1k Java : Human readable password generator
+6.3k Javascript : Generate random key with specific length
+12.2k Golang : Get remaining text such as id or filename after last segment in URL path
+14.4k Golang : How to pass map to html template and access the map's elements