Learning Go Programming by Unknown

Learning Go Programming by Unknown

Author:Unknown
Language: eng
Format: epub, mobi
Publisher: Packt Publishing


Array length and capacity

The built-in len function returns the declared length of an array type. The built-in cap function can be used on an array to return its capacity. For instance, in the following source snippet, the array seven of type [7]string will return 7 as its length and capacity:

func main() { seven := [7]string{"grumpy", "sleepy", "bashful"} fmt.Println(len(seven), cap(seven)) }

For arrays, the cap() function always returns the same value as len(). This is because the maximum capacity of an array value is its declared length. The capacity function is better suited for use with the slice type (discussed later in the chapter).



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.