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 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.

Golang – Best Parts – Part 1


In this series I will try to go through with golang  programming language tutorials and best parts of the language.

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

I will write parts as I learn and try to reference video tutorials and best practices as well.

Lets get started;

Its concurrency mechanisms make it easy to write programs that get the most out of multicore and networked machines, while its novel type system enables flexible and modular program construction.

Go compiles quickly to machine code yet has the convenience of garbage collection and the power of run-time reflection.

It’s a fast, statically typed, compiled language that feels like a dynamically typed, interpreted language.


If you are upgrading from an older version of Go you must first remove the existing version.

For example, if you installed Go to your home directory you should add the following commands to $HOME/.profile:


hello world

example below can be named different than “hello.go”. Just rename file accordingly and build should run.

gobin and goroot environment variables


go tool tour

%g replacement

In Go, a name is exported if it begins with a capital letter.


Multiple results

A function can return any number of results.

Naked/Named return statements should be used only in short functions, as with the example shown here. They can harm readability in longer functions.


Short variable declarations

Inside a function, the := short assignment statement can be used in place of a var declaration with implicit type

Type inference

Constants cannot be declared using the := syntax.


Go has only one looping construct, the for loop.


If you omit the loop condition it loops forever, so an infinite loop is compactly expressed.


Go’s if statements are like its for loops; the expression need not be surrounded by parentheses ( ) but the braces { } are required.


You probably knew what switch was going to look like.

A case body breaks automatically, unless it ends with a fallthrough statement.


A defer statement defers the execution of a function until the surrounding function returns.

The deferred call’s arguments are evaluated immediately, but the function call is not executed until the surrounding function returns.

Deferred function calls are pushed onto a stack. When a function returns, its deferred calls are executed in last-in-first-out order.