duancuan7057 2016-03-11 10:31
浏览 83
已采纳

我如何在go中找到数组的大小

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条回答 默认 最新

  • dongwenyou4298 2016-03-11 11:23
    关注

    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'

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题