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
+13.5k Facebook PHP getUser() returns 0
+7.1k Golang : Validate credit card example
+6k Golang : Convert Chinese UTF8 characters to Pin Yin
+11.6k Golang : Concurrency and goroutine example
+8.2k Android Studio : Rating bar example
+5.3k Golang : Pad file extension automagically
+4.7k Javascript : Access JSON data example
+19.1k Golang : When to use public and private identifier(variable) and how to make the identifier public or private?
+7.1k Javascript : How to get JSON data from another website with JQuery or Ajax ?
+12.1k Golang : Pagination with go-paginator configuration example
+5.6k Golang : Detect words using using consecutive letters in a given string
+5.6k Unix/Linux : How to find out the hard disk size?