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
+23.7k Golang : Find biggest/largest number in array
+11.8k Golang : Perform sanity checks on filename example
+10.7k Golang : Fix go.exe is not compatible with the version of Windows you're running
+13k Android Studio : Password input and reveal password example
+20.9k Golang : How to get time zone and load different time zone?
+16.1k Golang : Generate QR codes for Google Authenticator App and fix "Cannot interpret QR code" error
+22.6k Golang : Test file read write permission example
+9.4k Random number generation with crypto/rand in Go
+10.3k Golang : How to delete element(data) from map ?
+7.5k Golang : Trim everything onward after a word
+9.6k Golang : Ordinal and Ordinalize a given number to the English ordinal numeral