Golang – Best Parts – Part 2

Most of these examples and comments are taken from official documentation of golang. Please visit the website for full experience.

Go has pointers. A pointer holds the memory address of a variable.

Struct

Struct fields can be accessed through a struct pointer.

A struct literal denotes a newly allocated struct value by listing the values of its fields.

 

An array’s length is part of its type, so arrays cannot be resized. This seems limiting, but don’t worry; Go provides a convenient way of working with arrays.

A slice points to an array of values and also includes a length.

multidimensional arrays

Slices can be re-sliced, creating a new slice value that points to the same array.

Slices are created with the make function. It works by allocating a zeroed array and returning a slice that refers to that array:

To specify a capacity, pass a third argument to make:

It is common to append new elements to a slice, and so Go provides a built-in append function.

The range form of the for loop iterates over a slice or map.

You can skip the index or value by assigning to _.

A map maps keys to values.

Functions are values too. They can be passed around just like other values.

Go functions may be closures. A closure is a function value that references variables from outside its body. The function may access and assign to the referenced variables; in this sense the function is “bound” to the variables.

Leave a Reply

Your email address will not be published. Required fields are marked *