"index out of range" in Go

1

Managing arrays in Go I realize that there are two ways to declare them.

Form 1

When I declare the array with the fixed size

var a [5]int

a[0] = 1

The above does not give any problem.

Form 2

When I declare the array without the size.

var b []int

The above does not give any problem, but when I try to assign a value to it, like this:

b[0] = 1

This error appears:

panic: runtime error: index out of range
goroutine 1 [running]:
panic(0x5fb80, 0xc82000a0a0)
    /usr/local/go/src/runtime/panic.go:464 +0x3e6
main.main()
    /Users/my/path/main.go:8 +0x38
exit status 2

Why do I get that error?

You'll notice that the error message is a bit confusing given that it talks about goroutines , which I'm not using yet (I do not know what they are, I just know they exist).

    
asked by Gepser 24.03.2016 в 05:32
source

1 answer

4

Actually there are no two ways to declare an array in go , there is only one form, and it is the first of the two that in the example describes:

var a [5]int

In go, the length of the array must always be part of its type, this is because this data structure at runtime will never change its size, therefore the syntax to declare an array is:

var name [length]T

where length must be a constant of type% co_of non-negative% and T is the type of data it will contain. Obviously, once you have created the array of 5 elements of type int , initializing one of these fields is simple, the way you did it:

a[0] = 1

Now, in the second form you describe, you are not declaring an array, because the length is missing, but a data structure called slice . You can think of it as a structure that has a pointer to an array and a length. One of several ways to declare the slice is how you do it in form two:

var b []int

In the previous statement, you are declaring a slice named int that will point to an array of type b . Comparing syntax when declaring an array, if you do not specify the value of int , go assumes that you are creating a slice . Now, according to your example, you only declared the variable length of type b , but now you must initialize it, otherwise the value of this variable will be int (a type of data very similar to nil in conventional programming languages).

To initialize the value of a slice a possible way is to make use of the function null . This receives as parameter the type of data that you want to build, the initial length of this and an optional parameter that indicates the maximum value to which you can expand the capacity of the slice , as you will notice, this structure does allows to extend, or decrease, dynamically the length of the array. Therefore, its form 2 lacks the instruction:

//Construyo un slice de longitud inicial 5 y longitud máxima 10.
b = make([]int, 5, 10)

And now yes, you can assign values to the slice

b[0] = 1

The error shown when executing the code is somewhat obscure, because actually behind a slice there will always be an array, whether you create it with the function make() or build it from of an array that you have already created (such as the array make() in your code). In any case, according to its form 2, the initial length of the slice is nil, and when referring to the 0 object of this slice, it will generate a a and not an error similar to exceptions like index out of range in Java, because in reality NullPointerException only refers to the value of variables that are not initialized.

About the relationship with goroutine

A goroutine is a function that runs concurrently with the main thread. If you have programmed in Java you can think that they are similar to the concept of Threads , if not, think about this: your main program (the main thread of execution) is your main, or the one that executes when it runs the program that contains the code you need. This program can "delegate" tasks to be executed concurrently (at the same time) as the main program. In go, you achieve this through a goroutine. By its publication it indicates that it has no experience in this subject, so I invite you to look for more about this beautiful feature of go. Regarding your doubt, when the program finds that it tries to access an element of the array that does not exist, it throws an error of type 'index out of range'. In go, this error generates a call to the panic function, which aborts the execution of the program. When its trace indicates that the error occurred in the goroutine 1 it is indicating in which thread this error originated, but there is only one: the main one, which is always executed as a goroutine through the main.go method when launch the program.

    
answered by 24.03.2016 / 06:33
source