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
+8.6k Golang : Resumable upload to Google Drive(RESTful) example
+7.8k Golang : Capture text return from exec function example
+17.4k Golang : Set, Get and List environment variables
+21.1k Golang : untar or extract tar ball archive example
+11k Golang : List running EC2 instances and descriptions
+10.8k Golang : Print UTF-8 fonts on image example
+32.8k Golang : How to stream file to client(browser) or write to http.ResponseWriter?
+19.6k Golang : Create and resolve(read) symbolic links
+7k Golang : Append and add item in slice
+22.6k Golang : Generate MD5 checksum of a file
+19.2k Golang : Saving private and public key to files
+9.2k Golang : How to profile or log time spend on execution?