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
+21.4k Curl usage examples with Golang
+9.9k Golang : Turn string or text file into slice example
+19.9k Golang : Count JSON objects and convert to slice/array
+11.4k Golang : Concatenate (combine) buffer data example
+6.9k Golang : Decode XML data from RSS feed
+16.3k Golang : Loop each day of the current month example
+4.7k Unix/Linux : How to pipe/save output of a command to file?
+15.6k Golang : Force download file example
+8.9k Golang : GMail API create and send draft with simple upload attachment example
+28.2k Golang : Connect to database (MySQL/MariaDB) server
+21.8k SSL : How to check if current certificate is sha1 or sha2
+11.7k Golang : Calculations using complex numbers example