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
+6.2k Golang : Process non-XML/JSON formatted ASCII text file example
+5.1k Golang : Convert lines of string into list for delete and insert operation
+5.9k Golang : Shuffle array of list
+11.1k Google Maps URL parameters configuration
+19.8k Golang : Accept input from user with fmt.Scanf skipped white spaces and how to fix it
+13.8k Golang : Get dimension(width and height) of image file
+5.4k Golang *File points to a file or directory ?
+4.8k Javascript : How to get width and height of a div?
+12.1k Golang : Pagination with go-paginator configuration example
+6k PHP : How to check if an array is empty ?
+8.6k Golang : Combine slices but preserve order example
+8.8k Golang : Accept any number of function arguments with three dots(...)