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
+20.7k Android Studio : AlertDialog and EditText to get user string input example
+16.5k Golang : File path independent of Operating System
+13.8k Generate salted password with OpenSSL example
+46.2k Golang : Read tab delimited file with encoding/csv package
+15.3k nginx: [emerg] unknown directive "ssl"
+51.9k Golang : How to get time in milliseconds?
+8.1k Golang : Randomize letters from a string example
+25.3k Golang : Convert uint value to string type
+4.9k Nginx and PageSpeed build from source CentOS example
+5.6k Golang : Configure crontab to poll every two minutes 8am to 6pm Monday to Friday
+25.9k Golang : How to read integer value from standard input ?
+6.1k Fix ERROR 2003 (HY000): Can't connect to MySQL server on 'IP address' (111)