Golang builtin.cap() function example
package builtin
The cap built-in function returns the capacity of the input vector, according to the vector type:
Golang builtin.cap() function usage example
package main
import "fmt"
func main() {
array := []int{1,2,3,4,5,6,7,8,9,10}
fmt.Println(array[:]) // print everything
fmt.Println(array[0:5]) // print element 0 to 5. Remember, array starts counting from zero!
// there are times we do not know the size of the array during runtime
// so use cap() function
fmt.Println(array[:cap(array)]) // cap() returns the capacity/max size of the array
}
Output :
[1 2 3 4 5 6 7 8 9 10]
[1 2 3 4 5]
[1 2 3 4 5 6 7 8 9 10]
Reference :
Advertisement
Something interesting
Tutorials
+35.9k Golang : Integer is between a range
+9.9k Golang : ffmpeg with os/exec.Command() returns non-zero status
+6.6k Golang : Embedded or data bundling example
+22.1k Golang : Repeat a character by multiple of x factor
+17k Golang : How to save log messages to file?
+19.4k Golang : How to count the number of repeated characters in a string?
+5.2k Golang : The Tao of importing package
+29.1k Golang : Get first few and last few characters from string
+8.6k Golang : Add text to image and get OpenCV's X, Y co-ordinates example
+7.4k Golang : Hue, Saturation and Value(HSV) with OpenCV example
+14.5k Golang : How to check if your program is running in a terminal
+7.5k Golang : Shuffle strings array