如何查找数组的大小 [英] How do I find the size of the array in go

查看:148
本文介绍了如何查找数组的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过 len()函数,但它给出了声明的值。代码:

  package main 
var check [100] int
func main(){
println(len(check))
}

这里的输出是 100 ,我需要数组中的所有项目(即 0 )。

解决方案

数组Go是固定大小:一旦你在Go中创建了一个数组,你以后不能再改变它的大小。这是因为数组的长度是数组类型的一部分(这意味着类型 [2] int [3] int 是2种不同的类型)。这就是说,某些数组类型的值的长度总是相同的,并且由其类型决定。例如, [100] int 类型的数组值的长度总是 100 ,(可以使用查询内置函数 len() )。



规格:数组类型:


长度是数组类型的一部分;它必须评估为一个非负面的常量,该值可由类型的值表示, INT 。数组 a 的长度可以使用内置函数 len


如果您正在寻找设置了多少元素?的答案,这在Go中没有被跟踪。你要查找的数组中的总项目也总是与数组长度相同:在Go中创建数组时,数组中的所有元素都被初始化为零值的元素类型(除非另有规定,例如使用)。



例如,在这行后面:

pre $ var arr [100] int

数组 arr 已经有100 int s,全部都是 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 ,我们只指定第四个元素为 true ,它位于索引 3 )。



如果您询问有关切片的问题,您的问题可能会有更多含义。 :


切片是底层数组的连续段的描述符,并提供对编号序列的访问因此基本上,切片是数组某个(连续)部分的视图。切片标题或描述符包含一个指向它在数组中描述的部分的第一个值的指针,它包含一个长度和容量(这是长度可以扩展的最大值)。

我真的推荐阅读以下博客文章:

Go博客:切片:使用和内部元素



The Go Blog:数组,切片(和字符串):'append'的机制


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

解决方案

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

Spec: Array Types:

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 ints, 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'

这篇关于如何查找数组的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆