I have tried len()
function but it gives the declared value. The size()
function gives an error.
我尝试过len()函数,但它给出了声明的值。 size()函数给出错误。
Code:
package main
var check [100]int
func main() {
println(len(check))
}
The output is 100
here, I need the total items in array (i.e. 0
).
这里的输出是100,我需要数组中的总项目(即0)。
1 个解决方案
#1
18
Arrays in Go are fixed sizes: once you create an array in Go, you can't change its size later on. This is so to an extent that the length of an array is part of the array type (this means the types [2]int
and [3]int
are 2 distinct types). That being said the length of a value of some array type is always the same, and it is determined by its type. For example the length of an array value of type [100]int
is always 100
, (which can be queried using the built-in function len()
).
Go中的数组是固定大小的:在Go中创建数组后,您无法在以后更改其大小。这是因为数组的长度是数组类型的一部分(这意味着类型[2] int和[3] int是2种不同的类型)。也就是说,某些数组类型的值的长度始终相同,并且由其类型决定。例如,类型[100] int的数组值的长度始终为100(可以使用内置函数len()查询)。
规格:数组类型:
The length is part of the array's type; it must evaluate to a non-negative constant representable by a value of type
int
. The length of arraya
can be discovered using the built-in function len.长度是数组类型的一部分;它必须求值为由int类型值表示的非负常量。可以使用内置函数len发现数组a的长度。
If you're looking for the answer to "How many elements have been set?", that is not tracked in Go. The "total items in array" you're looking for is also always the same as the array length: when you create an array in Go, all elements in the array are initialized to the zero-value of the element's type (unless otherwise specified e.g. by using a composite literal).
如果您正在寻找“已设置了多少元素?”的答案,则不会在Go中跟踪。您正在寻找的“数组中的总项目”也始终与数组长度相同:在Go中创建数组时,数组中的所有元素都将初始化为元素类型的零值(除非另有说明)例如,通过使用复合文字)。
For example after this line:
例如在此行之后:
var arr [100]int
The array arr
already has 100 int
s, all being 0
(because that is the zero-value of type int
). After the following line:
数组arr已经有100个整数,都是0(因为这是int类型的零值)。在以下行之后:
var arr2 = [3]int{1, 2, 3}
The array arr2
has 3 int
elements, being 1
, 2
and 3
. And after the following line
数组arr2有3个int元素,分别为1,2和3.并在以下行之后
var arr3 = [...]bool{3: true}
The array arr3
has 4 bool
elements, being false
, false
, false
and true
(false
is the zero value of type bool
and we only specified the 4th element to be true
which is at index 3
).
数组arr3有4个bool元素,分别为false,false,false和true(false是bool类型的零值,我们只将第4个元素指定为true,它位于索引3)。
Your question might have more meaning if you would ask about slices:
如果您询问切片,您的问题可能会有更多意义:
A slice is a descriptor for a contiguous segment of an underlying array and provides access to a numbered sequence of elements from that array.
切片是底层数组的连续段的描述符,并提供对该数组中编号的元素序列的访问。
So basically a slice is a "view" of some (contiguous) part of an array. A slice header or descriptor contains a pointer to the first value of the part it describes in the array, it contains a length and the capacity (which is the max value to which the length can be extended).
所以基本上切片是数组的某些(连续)部分的“视图”。切片头或描述符包含指向它在数组中描述的部分的第一个值的指针,它包含长度和容量(可以扩展长度的最大值)。
I really recommend to read the following blog posts:
我真的建议阅读以下博文:
The Go Blog: Go Slices: usage and internals
Go Blog:Go Slices:用法和内部
The Go Blog: Arrays, slices (and strings): The mechanics of 'append'
Go Blog:数组,切片(和字符串):'追加'的机制
#1
18
Arrays in Go are fixed sizes: once you create an array in Go, you can't change its size later on. This is so to an extent that the length of an array is part of the array type (this means the types [2]int
and [3]int
are 2 distinct types). That being said the length of a value of some array type is always the same, and it is determined by its type. For example the length of an array value of type [100]int
is always 100
, (which can be queried using the built-in function len()
).
Go中的数组是固定大小的:在Go中创建数组后,您无法在以后更改其大小。这是因为数组的长度是数组类型的一部分(这意味着类型[2] int和[3] int是2种不同的类型)。也就是说,某些数组类型的值的长度始终相同,并且由其类型决定。例如,类型[100] int的数组值的长度始终为100(可以使用内置函数len()查询)。
规格:数组类型:
The length is part of the array's type; it must evaluate to a non-negative constant representable by a value of type
int
. The length of arraya
can be discovered using the built-in function len.长度是数组类型的一部分;它必须求值为由int类型值表示的非负常量。可以使用内置函数len发现数组a的长度。
If you're looking for the answer to "How many elements have been set?", that is not tracked in Go. The "total items in array" you're looking for is also always the same as the array length: when you create an array in Go, all elements in the array are initialized to the zero-value of the element's type (unless otherwise specified e.g. by using a composite literal).
如果您正在寻找“已设置了多少元素?”的答案,则不会在Go中跟踪。您正在寻找的“数组中的总项目”也始终与数组长度相同:在Go中创建数组时,数组中的所有元素都将初始化为元素类型的零值(除非另有说明)例如,通过使用复合文字)。
For example after this line:
例如在此行之后:
var arr [100]int
The array arr
already has 100 int
s, all being 0
(because that is the zero-value of type int
). After the following line:
数组arr已经有100个整数,都是0(因为这是int类型的零值)。在以下行之后:
var arr2 = [3]int{1, 2, 3}
The array arr2
has 3 int
elements, being 1
, 2
and 3
. And after the following line
数组arr2有3个int元素,分别为1,2和3.并在以下行之后
var arr3 = [...]bool{3: true}
The array arr3
has 4 bool
elements, being false
, false
, false
and true
(false
is the zero value of type bool
and we only specified the 4th element to be true
which is at index 3
).
数组arr3有4个bool元素,分别为false,false,false和true(false是bool类型的零值,我们只将第4个元素指定为true,它位于索引3)。
Your question might have more meaning if you would ask about slices:
如果您询问切片,您的问题可能会有更多意义:
A slice is a descriptor for a contiguous segment of an underlying array and provides access to a numbered sequence of elements from that array.
切片是底层数组的连续段的描述符,并提供对该数组中编号的元素序列的访问。
So basically a slice is a "view" of some (contiguous) part of an array. A slice header or descriptor contains a pointer to the first value of the part it describes in the array, it contains a length and the capacity (which is the max value to which the length can be extended).
所以基本上切片是数组的某些(连续)部分的“视图”。切片头或描述符包含指向它在数组中描述的部分的第一个值的指针,它包含长度和容量(可以扩展长度的最大值)。
I really recommend to read the following blog posts:
我真的建议阅读以下博文:
The Go Blog: Go Slices: usage and internals
Go Blog:Go Slices:用法和内部
The Go Blog: Arrays, slices (and strings): The mechanics of 'append'
Go Blog:数组,切片(和字符串):'追加'的机制