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
+10.1k Golang : Convert octal value to string to deal with leading zero problem
+5.6k Golang : Stop goroutine without channel
+4.5k Linux/MacOSX : Search and delete files by extension
+10.8k Golang : Resolve domain name to IP4 and IP6 addresses.
+3.9k Golang : Switch Redis database redis.NewClient
+8k Golang : How to feed or take banana with Gorilla Web Toolkit Session package
+13.8k Golang : Tutorial on loading GOB and PEM files
+20.2k Golang : How to run your code only once with sync.Once object
+11.3k Golang : Fix go.exe is not compatible with the version of Windows you're running
+5.8k Javascript : How to refresh page with JQuery ?
+6.7k Golang : Warp text string by number of characters or runes example