Day 2: Built-in Features

@mnmtanish
Bad Code
Published in
4 min readJul 30, 2015

--

Go language is a lightweight language compared to most others. The complete specification for the language can be written in a few pages. Go maintains its simplicity by only implementing features which are essential for most programmers. This tutorial consists of a number of example go programs demonstrating each feature. It is recommended to try some of them on the Go playground or on your local machine.

Constants

Constants are declared using the const keyword. They can be declared with a data type but it’s not common. The “Hello World” string can be stored in a constant and printed.

Variables

Variables can be declared using the var keyword. The “Hello World” string can be stored in a variable before printing.

The first 2 lines in the function can be shortened with the “:=” operator. This will attempt to identify the type form the value and use it. In most cases this will work as expected but when using numbers, it will always use int for integers and float64 for floating point numbers. When writing Go programs, the := operator is preferred more than declaring with var and assigning later.

Please note that all variables created must be used in the program otherwise it’ll give an error when running/compiling. In some cases the “_” can be used in place of a variable.

Go has many more data types which can be read from the go specification. The go spec is shorter than most others and also very easy to read and understand.

Functions

Functions can be defined in Go using the func keyword. The hello world program can be written using a sayHello function.

Go functions can also get a number of arguments and use them.

Go functions can also return multiple arguments.

Go functions can be used as values. They can be stored in variables, passed as arguments and returned from other functions.

Above example also demonstrates why is most commonly known as a closure. The function created inside getGreeter captures surrounding variables (in this example, the prefix variable) and remembers it when it’s used later.

Control Statements

Go only has a few statements to control program flow. They are if, switch, for, break, continue and goto. Go also has a select statement which is best explained after getting used to go-routines and basics of concurrent programming in go.

If

In Go control statements do not use parentheses.

If conditions may also have an else block.

Go also supports else-if blocks. In some cases, a switch may be more appropriate than long if-else ladders.

switch

Above example can be written using a switch statement. A switch can be used with strings, integers and other types.

The switch can be written in another format which is useful for more complex situations.

Printing error messages like this is a really bad idea

for

Go only has for loops but it has a few forms. For loops can be used in many ways depending on the situation. The first form is the infinite loop. The break keyword can be used to exit the loop when necessary.

The second form is similar to the while loop in most languages.

Above loop can be written using the more common for loop like form.

There are 2 more special cases which is explained under Slices and Maps.

Data Structures

Go only has a few data structures built into the language but they are flexible enough to more complex ones on top of them.

Slices

Go arrays are fixed length therefore not easy to handle. Therefore, go programmers often use slices to store series of data. A slice can be created using the make keyword. In this example the Println function inside the fmt package is used. fmt.Println can print more data types than println. Packages will be explained more in a future tutorial.

Slices can also be created with an initial set of elements. Additional items can be added to a slice using the append function. Please note that the array used for the slice may or may not change which depends on the slice’s capacity (array size).

The for loop has a special form to iterate over a slice.

The copy function can be used to copy data form one slice to another.

A slice can be made from another slice. Both slices still use the same array. Therefore any changes made to one slice will reflect on the other.

To learn more about how slices work, read go slice usage and internals on golang blog.

Maps

Go map keys can be strings, integers, floats, structs or pointers to data types. This can be useful for mapping one value to another with many data types. The same make keyword can be used to create an empty map.

A map can be created with an initial set of items. Unlike slices, maps are copied if assigned to a new variable.

For loop has a special form for maps to iterate over keys and values. The order keys are accessed is randomised by Go to make sure that developers do not depend on it accidentally.

Structs

Go structs are a collection of variables with one or more data types.

Structs may also have methods which makes it similar to classes in OOP languages. To add methods to structs, they must be declared outside the main function.

What’s Next ?

This tutorial only covers the most common parts of golang built-ins. To read more the best place is the golang spec. It’s also the best place to look when there’s a question regarding how go works.

--

--