The book “Head First. Learning Go »

imageHey. Habrozhitel! Go makes building simple, reliable, and efficient programs easy. And this book will make it accessible to ordinary programmers. The main goal of Go is to work effectively with network communications and multiprocessing, but the code in this language is written and read no more difficult than in Python and JavaScript. Simple examples allow you to get acquainted with the language in action and immediately start programming on Go. So you quickly learn the generally accepted rules and techniques that will allow you to call yourself a gopher.


Zero values ​​in arrays


As in the case of variables, when creating arrays, all the values ​​contained in them are initialized to zero for the type contained in the array. So the array of int values ​​is filled with zeros by default:

image

On the other hand, the null value for strings is an empty string, so by default the array of string values ​​is filled with empty strings:

image

Zero values ​​allow you to safely perform operations with elements of arrays, even if they have not been assigned values. For example, the following array stores integer counters. Any element can be increased by 1 even without first assigning a value, because we know that all counter values ​​start at 0.

image

When creating an array, all elements contained in it are initialized to zero for the type stored in the array.

Array literals


If you know in advance the values ​​that should be stored in the array, you can initialize the array with these values ​​in the form of an array literal. An array literal begins as an array type — with the number of elements in square brackets followed by the element type. Next, in curly brackets is a list of the initial values ​​of the elements of the array. Element values ​​must be separated by commas.

image

These examples are almost no different from the previous ones, except for the fact that instead of sequentially assigning values ​​to the elements of the array, the entire array is initialized using the array literal.

image

Array literals also allow you to use short variable declarations with: =.

image

Array literals can span multiple lines, but there must be a comma before each line break in the code. The comma should even be after the last element in the array literal, if followed by a line break. (At first glance, this syntax looks awkward, but it simplifies the subsequent addition of new elements in the code.)

image


image


Functions of the “fmt” Package Can Work with Arrays


When you are debugging code, you do not need to pass the elements of the Println arrays to the other functions of the fmt package one by one. Just pass the whole array. The fmt package contains the logic for formatting and outputting arrays. (The fmt package also works with segments, maps, and other data structures, which will be described later.)

image

You may also remember the verb "% # v" used by the Printf and Sprintf functions - it formats the values ​​as they appear in Go code. When formatting with "% # v", arrays are displayed in the form of Go array literals.

image

Accessing array elements in a loop


You are not required to explicitly write the integer indices of the elements of the arrays that you access in your code. You can also use the value of an integer variable as an index.

image

This means that array elements can be iterated over in a for loop. The loop iterates over the indexes of the array, and the loop variable is used to access the element with the current index.

image

When accessing the elements of arrays through a variable, you must act carefully and monitor what index values ​​are used in the program. As mentioned earlier, arrays contain a specific number of elements. An attempt to access the index outside the array leads to panic - an error that occurs during program execution (and not at the compilation stage).

image

Typically, in a panic situation, the program crashes with an error message for the user. Needless to say, such situations should be avoided whenever possible.

image

Checking the length of an array with the len function


Writing loops that are limited only by the correct indexes carries a certain risk of errors. Fortunately, there are a couple of tricks that simplify this process.

First, you can check the actual number of elements in the array before accessing the element. To do this, you can use the built-in len function, which returns the length of the array (the number of elements contained in it).

image

In the processing loop of the entire array, you can use the len function to determine which indexes can be accessed safely.

image

image

Safe enumeration of arrays in the "for ... range" loop


In another, even safer way of processing all elements of an array, a special for ... range loop is used. In the form with range, a variable is specified for storing the integer index of each element, another variable for storing the value of the element itself and an iterated array. The loop is executed once for each element in the array; the element index is assigned to the first variable, and the element value is assigned to the second variable. A code is included in the loop block to process these values.

image

This form of the for loop does not contain confusing expressions of initialization, condition, and termination. And since the element value is automatically assigned to the variable, the risk of accessing an invalid array index is eliminated. The for loop form with range is easier to read and safer, which is why it most often occurs when working with arrays and other collections.

The following is an example with the output of all values ​​from an array of notes, converted to use a for ... range loop:

image

The loop runs seven times, once for each element in the notes array. For each element, index is assigned the index of the element, and note is assigned the value of the element. After that, we print the index and value.

»More information about the book can be found on the publisher’s website
» Contents
» Excerpt

For Khabrozhiteley 25% discount on the coupon - Head First

Upon the payment of the paper version of the book, an electronic book is sent by e-mail.

All Articles