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
+3.9k JQuery : Calling a function inside Jquery(document) block
+33.4k Golang : Upload and download file to/from AWS S3
+10.4k How to tell if a binary(executable) file or web application is built with Golang?
+28.6k Golang : Remove characters from string example
+5.1k Golang : Debug with Godebug
+10.6k Golang : How to detect a server/machine network interface capabilities?
+5.3k CodeIgniter : form input set_value cause " to become & quot
+21.1k Golang : Set and Get HTTP request headers example
+8.7k Golang : Check a web page existence with HEAD request example
+8.9k Golang : Use regular expression to get all upper case or lower case characters example
+4.9k Golang : Function as an argument type example
+13.3k Golang : Get URI segments by number and assign as variable example