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
+19.6k Golang : Get current URL example
+9.4k Golang : Scramble and unscramble text message by randomly replacing words
+14.4k Golang : How to pass map to html template and access the map's elements
+5.6k PHP : Fix Call to undefined function curl_init() error
+11.1k Golang : Read until certain character to break for loop
+14.2k Golang : syscall.Socket example
+27.5k Golang : Convert integer to binary, octal, hexadecimal and back to integer
+12.4k Elastic Search : Return all records (higher than default 10)
+8.3k Golang : Auto-generate reply email with text/template package
+12.7k Golang : Add ASCII art to command line application launching process
+5.6k Golang : Detect words using using consecutive letters in a given string
+14.8k Golang : Adding XML attributes to xml data or use attribute to differentiate a common tag name