LEN function is used to return the length or number of elements of arrays, slices, maps, strings and other types. The exact functionality depends on the type passed to the len() function. Detailed introduction: 1. For arrays and slices, the len() function returns the number of elements of the array or slice; 2. For mapping, the len() function returns the number of key-value pairs in the mapping; 3. For strings, len() The function returns the number of bytes of the string (that is, the number of characters in the string).
The operating system for this tutorial: Windows 10 system, go1.20.1 version, Dell G3 computer.
In the Go language, the len() function is used to return the length or number of elements of arrays, slices, maps, strings and other types. The exact functionality depends on the type passed to the len() function.
1. For arrays and slices, the len() function returns the number of elements in the array or slice.
arr := [3]int{1, 2, 3} fmt.Println(len(arr)) // 输出为 3 slice := []int{1, 2, 3, 4, 5} fmt.Println(len(slice)) // 输出为 5
2. For mapping, the len() function returns the number of key-value pairs in the mapping.
m := map[string]int{"a": 1, "b": 2, "c": 3} fmt.Println(len(m)) // 输出为 3
3. For strings, the len() function returns the number of bytes of the string (that is, the number of characters in the string).
str := "Hello, 世界" fmt.Println(len(str)) // 输出为 13,因为包含一个英文逗号和一个中文字符
In short, the len() function is used to obtain the length or number of elements of different types of data. It is a very commonly used built-in function in the Go language.
The above is the detailed content of What is the LEN function?. For more information, please follow other related articles on the PHP Chinese website!