16

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

icza
  • 389,944
  • 63
  • 907
  • 827
Revanth Penugonda
  • 371
  • 1
  • 2
  • 6
  • it's not C, in Go the elements of array are initialized. –  Mar 11 '16 at 10:34
  • 13
    What else than 100 could `len` return for an array of length 100? – Volker Mar 11 '16 at 11:04
  • 9
    Please do the go tour. It's fun I promise. https://tour.golang.org/ – voutasaurus Mar 11 '16 at 17:23
  • @Volker, actually he want to say the actual number of elements in array. In java if we declare `List a = new ArrayList(10);` and print `a.size()` then it gives 0 and if we add two element using `a.add()` and check for `a.size()` it will give size 2. consider this Go program https://play.golang.org/p/Zc6sFI8wAl, read comments below the program which describes problem – Amogh Jul 28 '17 at 13:48
  • @Volker If your program changes, would you like to manually change 100 to 101 in a thousand places, or just in one place? – HoldOffHunger Jul 06 '18 at 23:03

1 Answers1

31

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'

icza
  • 389,944
  • 63
  • 907
  • 827
  • In this case consider a scenario where I want to write logic depending upon elements in array. consider this Go program https://play.golang.org/p/Zc6sFI8wAl, read comments added below the program which will describe my problem. – Amogh Jul 28 '17 at 13:33
  • @Amogh 2 things: First, you're not changing anything in the array as that code is also commented out. Second, if you do change some elements but not all, there will still be some elements being 0. And if you change other elements to be positives, the minimum / lowest will still be the 0, so that is the correct output. – icza Jul 28 '17 at 14:10
  • Yes, perfect I got that. so that means there is noting like in Go to check whether elements are added or not in an array/slice. Because don't consider the simple progrm to find smallest number. consider a service method which finds some Ids of somthing from DB and adds in array/slice and next we iterate over that array to perform somthing. In that case if IDs are not found by first process then also second process will iterate over with value 0, right? – Amogh Jul 28 '17 at 14:36
  • Like, what we do in java, we declare List of int as `List a = new ArrayList(100);` and adds some Id's in it using `a.add()` now consider I have added only 10 Ids and started a for loop till i – Amogh Jul 28 '17 at 14:37
  • What I feel, we should NOT use array representation where we want to check how many elements are actually added in it. We should use slice by declaring `len` as 0 for eg. `var aList = make([]int,0,100)` by this `len(aList)` gives so we can check if elements are really added or not. – Amogh Jul 28 '17 at 14:56
  • @Amogh Arrays have their uses. Check out this answer: [Why have arrays in Go?](https://stackoverflow.com/questions/38645175/why-have-arrays-in-go/38645895#38645895) If that doesn't answer your question, post it as a new question. – icza Jul 28 '17 at 15:07