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.