Home > Backend Development > Golang > What is the LEN function?

What is the LEN function?

小老鼠
Release: 2023-12-14 15:48:49
Original
3527 people have browsed it

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).

What is the LEN function?

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
Copy after login

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
Copy after login

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,因为包含一个英文逗号和一个中文字符
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template