Driving 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 so:
b[0] = 1
I get this error:
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 since it talks about goroutines
, which I'm not using yet (I don't even know what they are, I just know they exist).
Actually there are not two ways to declare an array in
go
, there is only one way, and it is the first of the two that the example describes:In go, the length of the array must always be part of its type, this is because this data structure will never change its size at runtime, therefore the syntax to declare an array is:
int
where length must be a non-negative type constant and T is the data type that it will contain. Obviously, once the array of 5 elements of type is createdint
, initializing one of these fields is simple, the way you did it:Now in the second way 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:
In the statement above, you are declaring a named slice
b
that will point to an array of typeint
. Compared to the syntax when declaring an array, if the value of is not specifiedlength
, go assumes that you are creating a slice . Now, according to your example, you just declared the variableb
of typeint
, but now you must initialize it, otherwise the value of this variable will benil
(a data type very similar to thatnull
in conventional programming languages).To initialize the value of a slice , one possible way is to use the
make()
. This receives as a parameter the type of data that it wants to build, its initial length and an optional parameter that indicates the maximum value to which it can expand the capacity of the slice , as you will notice, this structure does allow you to expand, or decrease, dynamically the length of the array. Therefore, your form 2 is missing the statement:And now yes, you can assign values to the slice
The error displayed when executing the code is somewhat obscure, because in reality there will always be an array behind a slice , whether you create it with the function
make()
or build it from an array you have already created (such as the arraya
in your code). In any case, according to its form 2, the initial length of the slice is nil, and when referencing object 0 of this slice, it will generateindex out of range
an error and not an exception-like error likeNullPointerException
in Java, because it actuallynil
just references the value of variables that are not initialized.On the relationship with goroutine
A goroutine is a function that is executed concurrently with the main thread. If you have programmed in Java you can think of them as similar to the concept of Threads ., if not, think about this: your main program (the main execution thread) is your main, or the one you execute when you run the program that contains the code you need. This program can "delegate" tasks to run concurrently (at the same time) as the main program. In go, you accomplish this through a goroutine. By your post you indicate that you have no experience in this topic, so I invite you to search more about this beautiful feature of go. Regarding your question, when the program finds that it is trying to access an array element that does not exist, it throws an 'index out of range' error. In go, this error generates a call to the panic function, which aborts the execution of the program.