英文:
How do I find the size of the array in go
问题
我已经尝试了len()
函数,但它返回的是声明的值。size()
函数会报错。
代码:
package main
var check [100]int
func main() {
println(len(check))
}
这里的输出是100
,我需要数组中的总项数(即0
)。
英文:
I have tried len()
function but it gives the declared value. The size()
function gives an error.
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
).
答案1
得分: 31
在Go语言中,数组是固定大小的:一旦你在Go中创建了一个数组,就无法在以后改变它的大小。这意味着数组的长度是数组类型的一部分(这意味着类型[2]int
和[3]int
是两种不同的类型)。也就是说,某个数组类型的值的长度始终是相同的,并且由其类型确定。例如,类型为[100]int
的数组值的长度始终为100
(可以使用内置函数len()
查询)。
如果你想知道"有多少元素已设置"的答案,那在Go中是没有追踪的。你所寻找的"数组中的总项目数"也始终与数组的长度相同:当你在Go中创建一个数组时,数组中的所有元素都会被初始化为元素类型的零值(除非另有指定,例如使用复合字面量)。
例如,在以下代码行之后:
var arr [100]int
数组arr
已经有了100个int
,全部都是0
(因为0
是int
类型的零值)。在以下代码行之后:
var arr2 = [3]int{1, 2, 3}
数组arr2
有3个int
元素,分别是1
、2
和3
。在以下代码行之后:
var arr3 = [...]bool{3: true}
数组arr3
有4个bool
元素,分别是false
、false
、false
和true
(false
是bool
类型的零值,我们只指定了第4个元素为true
,即索引为3
)。
如果你的问题更多地涉及到slice(切片),可能会有更多意义:
切片是对底层数组的一个连续片段的描述符,并提供对该数组中一系列编号的元素的访问。
因此,切片基本上是数组的某个(连续的)部分的"视图"。切片头部或描述符包含指向所描述部分在数组中的第一个值的指针,它包含一个长度和一个容量(长度可以扩展到的最大值)。
我真的建议阅读以下博文:
英文:
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()
).
> 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 array a
can be discovered using the built-in function len.
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).
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:
var arr2 = [3]int{1, 2, 3}
The array arr2
has 3 int
elements, being 1
, 2
and 3
. And after the following line
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
).
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
The Go Blog: Arrays, slices (and strings): The mechanics of 'append'
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论