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
+14.2k How to automatically restart your crashed Golang server
+7.8k Golang : Handle Palindrome string with case sensitivity and unicode
+38.9k Golang : How to read CSV file
+8.9k nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
+7.9k Golang : Multiplexer with net/http and map
+11.5k Golang : convert(cast) float to string
+10.3k Golang : Generate 403 Forbidden to protect a page or prevent indexing by search engine
+5.8k Golang : Extract unicode string from another unicode string example
+11.4k CodeIgniter : Import Linkedin data
+6.6k Android Studio : Hello World example
+11.9k Golang : Split strings into command line arguments
+5.4k Golang : Frobnicate or tweaking a string example