Golang Beginner's Guide: Your First Steps

Jacek Majda03/15/2024

go beginner's guide

Although currently rated in 12th position in TIOBE Programming Community Index, Go has had its ebbs and flows throughout the years, and is still gaining popularity.

Once widely adopted at Google where it was first developed, it’s making rounds in the developer community as a must-learn for anyone who wants to work at Google or dive deeper into Google tools. One thing is indisputable: Go has a lot to offer thanks to its clear syntax and robust standard library.

In this part of our Go blog series, I’m set to guide you through your first steps in Golang and the basics of this programming language.

For more information on how and why Go was created and how it compares to its long-time rival, React, visit previous parts of the series:

Golang 101: All the Basics You Need to Know
Go vs. Python: Key Differences Explained

What Do You Need to Start Using Go?

For all its intricacy, Go is relatively easy to learn thanks to one feature: a minimalistic syntax. It’s limited to a few crucial statements that you can readily memorize. This also means that the syntax is clean and you can follow it without any issues and not spend too much time looking things up as you learn.

Similarly to C and C++, Go is also a compiled language which means that you need to run source code through a compiler - this feature makes Golang much faster than Javascript.

As you make your first steps in Golang, you have two options on how to develop your skills:

  • By installing software on your gear - to do that, download and install GO itself on your machine. This option is covered below in step #1- also you will need any code editor. For this purpose, I can recommend Visual Studio Code which is widely used by developers + plugin GO extension that can be installed from vs code itself that will help with auto imports, code snippets, etc.
  • By using Golang playground (go.dev/play) - which allows you to code online right away, but there are certain limitations and you will be able to write just simple scripts. From my perspective, the playground will be enough to just start and learn the simple basics of Go.

Go Features - an Overview

Go is a statically typed programming language that helps reduce the number of bugs and reduces the time spent on debugging. Additionally, type declarations serve as automatically-checked documentation. Here are a few more features of Golang:

  • Goroutines - the most famous Go feature which allows you to run concurrent processes/functions simultaneously.
  • Garbage collection - you don’t need to worry about memory management, GO will do it for you.
  • Go is open-source - this means that once you’re more familiar with this programming language, you might be able to fix bugs and improve it for other Go developers.
  • Testing capability - with Go, you can write unit tests at the same time as you write code for your application.
  • Extensive standard library - the packages are readily available and will help you cover some functions and contents.
  • Tools for coding efficiency - Godoc, Gorun, Gomft will be crucial in ensuring that your code is readable, properly documented, and can be run and experimented with.

Why Is It Worth Learning Go?

You may wonder, why should you learn Go at all. 

That’s a very good question. After all, there are hundreds of computer programming languages, old and still in use such as Cobol or C, and new ones like Reason. Golang is a relatively new language (read about its origin story in our Golang - All the Basics that You Need to Know article), but it’s already mature and widely used by large companies - of course, the largest being Google, the creators of this language. 

You can also check a case study indicating usage of this language by Google:

New Case Studies About Google’s Use of Go

Go has turned out to have a much broader reach than we had ever expected. Its growth in the industry has been phenomenal, and it has powered many projects at Google.

Okay, but why should you learn Go, to be specific? Here are some of the reasons: 

It’s fast: Check benchmarks for the most popular server-side languages such as Node.JS, Python, Java, and PHP.

It’s simple: in order to write a concurrent function you just need to add the `go` keyword before the function.

It’s lightweight: goroutines (threads) take only 8 kilobytes and you can have thousands of them.

Zero dependency to write web server -you will need just the standard library `http` and a few lines of code (extra tip: try this in Node.JS).

How to Start Programming in Go?

Step #1: Install Go locally or use The Go Playground

Now you know why you should learn and use Go, let’s get coding. Our first step will be installing Go - here’s how to do it on different operating systems.

How to install Go on Windows

Here's detailed documentation: https://go.dev/doc/install

  1. Download Go file: https://go.dev/dl/go1.19.windows-amd64.msi.
  2. Open the MSI file you downloaded and follow the prompts to install Go.
  3. Verify that you've installed Go.
  • In Windows, click the Start menu.
  • In the menu's search box, type cmd, then press the Enter key.
  • In the Command Prompt window that appears, type the following command:
     $ go version< 
  • Confirm that the command prints the installed version of Go.

How to install Go on OSX

  1. Download Go file: https://go.dev/dl/go1.19.darwin-amd64.pkg.
  2. Open the package file you downloaded and follow the prompts to install Go.
    The package installs the Go distribution to /usr/local/go. The package should put the /usr/local/go/bin directory in your PATH environment variable. You may need to restart any open Terminal sessions for the change to take effect.
  3. Verify that you've installed Go by opening a command prompt and typing the following command:
     $ go version 
  4. Confirm that the command prints the installed version of Go.

How to install Go on Linux

  1. Download Go file: https://go.dev/dl/go1.19.linux-amd64.tar.gz
  2. Remove any previous Go installations by deleting the /usr/local/go folder (if it exists), then extract the archive you just downloaded into /usr/local, creating a fresh Go tree in /usr/local/go:
     $ rm -rf /usr/local/go 
    tar -C /usr/local -xzf go1.19.linux-amd64.tar.gz 

    (You may need to run the command as root or through sudo).
    Do not untar the archive into an existing /usr/local/go tree. This is known to produce broken Go installations.
  3. Add /usr/local/go/bin to the PATH environment variable.
    You can do this by adding the following line to your $HOME/.profile or /etc/profile (for a system-wide installation):
     export PATH=$PATH:/usr/local/go/bin 
    Changes made to a profile file may not apply until the next time you log into your computer. To apply the changes immediately, just run the shell commands directly or execute them from the profile using a command such as a source $HOME/.profile.
  4. Verify that you've installed Go by opening a command prompt and typing the following command:
     $ go version 
  5. Confirm that the command prints the installed version of Go.

Step #2: Write your first ‘Hello World!’ program to test it out

Alright, you are good to go and ready to write your first application.

If you are using Go on your local machine, create a file with `go` extension (example.go) and open it via VS Code. If you are using go playground erase all default code from there.

Our app will look like below:

 
```
package main

import "fmt"

func main() {
	fmt.Println("Hello World")
}
      
```

But let's see in more detail what is going on there!

First, we need to add the package name, because every Go app consists of packages. The entry and the most important package indicate from where Go should start an app, executing code, so write on the top of file: `package main`

Then we need to import the standard library build into Go itself and it’s called `fmt` - this package simply allows you to print whatever you would like to the variable, output (terminal, file, etc.), so type: `import “fmt”`.

Now we need our main function that will execute code that we will write, and again the function needs to be called `main`. This is how Go will know that it should start code execution from here, so type: `func main() {}`

In order to print the words” `Hello World` we need to use our imported library and method called: `Println` this will just simply print line to our default output which is terminal, so inside the main function type:

 ```
package main

import "fmt"

func main() {
	fmt.Println("Hello World")
}
      
```

Yes, you’ve just created your first app! In order to see what will produce and if it is working - also if you are using Go playground - click the `format` button. It will automatically check if formation is adequate and if there aren’t any errors in a code. Once the code is checked and you don’t need to fix any issues, then simply click the `run` button. You will see the output on the terminal below.

Screenshot 2022-11-17 at 09.46.41

If you are using a local environment in a VS Code in the built-in-terminal type: `go run main.go`. The program will then execute and you should have the same result in your local terminal.

Step #3: Write more code - what to do next?

Congrats, you’ve successfully built your very first Go program!

Next Steps

The next step you should take is to do some more tutorials (text or video) there are many of them, including the free ones on the official go website or the unofficial `Free Code Camp` Youtube channel or paid tutorials on Udemy.com. Below you can find a couple of useful links:

A Tour of Go

Learn Go Programming - Golang Tutorial for Beginners

Learn Go Programming by Building 11 Projects – Full Course

Go by Example

Codecademy - Golang

If you have any problems or questions, you can also join the wonderful Golang community on Slack: Gophers.