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
+4.3k Javascript : How to show different content with noscript?
+4.7k JavaScript: Add marker function on Google Map
+13.5k Golang : Read XML elements data with xml.CharData example
+15k Golang : Search folders for file recursively with wildcard support
+22.5k Golang : Convert Unix timestamp to UTC timestamp
+5.6k Swift : Get substring with rangeOfString() function example
+25.3k Golang : Convert uint value to string type
+12.7k Golang : Pass database connection to function called from another package and HTTP Handler
+24.5k Golang : Time slice or date sort and reverse sort example
+4.9k JQuery : Calling a function inside Jquery(document) block
+12k Golang : Clean formatting/indenting or pretty print JSON result
+6.5k Golang : Spell checking with ispell example