Learn Golang in one blog

Learn Golang in one blog

Go is a simple, fast, and concurrent programming language. Its simplicity in design makes it an amazing programming language to work with. Go is currently gaining a lot of popularity, and a lot of organizations now prefer to write their backend in Go. More importantly, all Cloud Native and some Blockchain projects are written or being written in Go, some popular tools are Kubernetes, Prometheus, and Docker.

I wanted to learn Go as fast as I could, but could not find a very fast learning resource that summed up things quickly, all though this blog is introductory, it will cover all the basics that you need to get started writing Go as quickly as possible, assuming you already have prior programming experience.

Set up your environment

Head over to golang.org and grab the latest copy of Go. The easiest way is to download and install the binary on your preferred operating system. you’re free to build from source or use brew install go if you’re on macOS. Once you’ve done that you’ll have to Go available in your command line. Typing go will show the available commands.

Go enforces a structure to your projects, the idea is that all of your projects should live under one roof to access all of Go’s features. By default, this is under $HOME/go, you can print to console by using go env GOPATH. You can set this location to be anywhere on your computer. the default structure of the directory will be,

Create your first project in the src/ folder and you’re ready to go!

Packages and Imports

Go does not support Classes like in OOPs programming languages such as Java, it uses the package system instead. Each package is a directory in your workspace, and each go file must belong to some package. Hence, each file should start with the keyword package followed by the package name. A go executable must contain package main.

Go uses relative imports to import packages into the current file. The relative path is usually $GOPATH/src since most packages are stored in the pkg directory. A package can be imported by using the import keyword followed by the list of packages inside parenthesis.

The standard library comes preinstalled, with Go, and contains the most essential and useful packages. "fmt" is used to export Println which prints to console. Go does not allow unused imports.

Variables

Go’s basic primitive types are bool, string, int, uint, float, and complex, the size of the type can be specified next to the type, uint32. A variable is declared by the var keyword followed by the variable name and the type.

variables can also be initialized with a shorthand notation := as Go can infer the type. Just like imports, unused variables are not allowed.

Also, Go does not use semicolons to end a statement.

An important point to note is how Go scopes variables with a package, a variable is public if the first letter is in Captial, else private, same goes for functions.

Functions

Functions are an essential part of Go, and of course, the above won’t work as execution has to happen in a function body. Functions are declared with the keyword func followed by the function name, arguments, and return type. A Go application must contain the main function which is the entry point to the application. It does not take any arguments or return anything. The opening braces of the function must start at the same level as the function and cannot move to the new line.

function parameters are declared with their name followed by the type and separated by a comma. The return type must be provided if the function returns, as a shortcut the return variable can also be declared to avoid declaring another variable inside the function, here’s an example.

Arrays, Slices, and Maps

Arrays can be declared by simply specifying the datatype next to brackets with an integer denoting the size of the array. Then, arrays can be assigned by their index, a more convenient way to initialize is to use the shorthand syntax along with the data in parenthesis.

But there’s a problem here. You cannot modify the length of the array, wouldn't it be more convenient when you don’t know the size yet? That’s where Slices come in, slices are simply dynamic arrays. you can declare a slice just like arrays, without specifying the size.

Slices can be really useful in performing a lot of operations. The copy or append function can be used to manipulate the slice. Slices can also be concatenated using the append with the spread operator(…). A slice can be sliced using its indices within the brackets. Below are some examples.

The append function does not modify the slice but returns a new slice from the given one. Here’s the output.

Maps are equivalent to a HasMap in Java or a Dictionary in python. They store key-value pairs. A map can be created using the make keyword followed by the keyword map and the datatype of the key in brackets and value next to it.

Maps are simple to operate on, they can be assigned values by using the [ ] operator specifying the key and value, and a key can be removed by using the delete function.

Loops

Loops in Go exist in the simplest form, there is only one looping syntax, the for loop. The for loop can be written in multiple ways to meet your looping needs. The first syntax is a familiar one starting with the pointer variable i and followed by condition and incrementation. The below example will print 1 to 5.

Oh! you badly miss the while loop? Don’t worry, Go has you covered, all you have to do is mention the condition with the for loop and use a pointer declared outside the loop just how you would when using a while loop.

The range function provides an easy way to access the index as well as value.

Struct

The struct keyword is to define a shape to your data. Since Go does not support classes, data of a certain shape requirement can be stored in variables of that type of struct. A struct is created using the keyword type and its properties can be accessed by the dot(.) operator.

nil, error, and multiple return values

Go provides some smooth ways to handle the error and nil values. Both error and nil are native built-in types that can be used to perform validation before performing some operation. Go also supports returning multiple types from a function, this can be done using specifying the type within parenthesis in place of the return type.

errors or nil can be returned depending on the operation performed using an if check. Here’s an example showing how you can handle errors by checking the input for a square root function.

Pointers

Pointers in Go are similar to pointers in other languages, you can refer to the memory address of the variable by prefixing the variable with an ampersand(&) symbol and dereference it using an asterisk(), by default, go passes arguments by value not reference, you can accomplish this by prefixing that type of the argument in the function with an **asterisk()**, here’s an example.

without the & it’d print 5 as a copy of the variable would have been passed, and once we have the reference, we need to deference the memory to get the value by using * on the variable again.

That’s it! you should be no ready to write your first program in Go, try practicing the code snippets and if you feel like challenging yourself more, try writing a basic HTTP server using the “net” package. This should give you enough practice to write some awesome packages or contribute to your favorite Go repository.

Here are some additional resources if you wish to dive deeper.

Follow me for more updates as I plan to cover concurrency and other special features in Go next. I am also keeping track of my sample projects here, feel free to check it out and give a star and leave some claps if you liked this 😁

Connect with me! I’m up for a chat.

Twitter

Linkedin