DEV Community

Cover image for Setting up our Go Development Environment
Will Velida
Will Velida

Posted on

Setting up our Go Development Environment

I've been wanting to learn Go for a while now. I've tried and failed to keep my learning consistent, so I've decided to force myself to keep on top of my learning through a couple of avenues:

  1. This Blog I've set up.
  2. This GitHub repo.

In that GitHub repo, I'll be adding content on both Golang itself and building applications with Go that use Azure. I'll be building this repo up over time, but if you have a particular sample or scenario that you'd like me to create, please feel free to raise an issue.

This article is aimed at developers who have never used Go before and want to set up their environment to start playing around with the langugage. In this article, I will cover:

  • Why Go?
  • Installing Go on our machine
  • Verifying Go has been installed correctly
  • Installing the Go extension on VS Code
  • Building and Running a basic Hello World app!

One thing to note before we start is that I'll be doing my development on a Windows machine, so commands and formatting will be tailored towards the Windows ecosystem. If you have a Mac, I will do my best to try and include instructions for Mac as I go along, but I may miss out from time to time. If you need guidance for Mac systems, please reach out to me on Twitter and I'll do my best to help out 😊

Why Go?

Go is an open source programming language designed for building simple, fast and reliable softwae. It's expressive, clean, efficient and concise. It compiles quickly to machine code and also has garbage collection.

For cloud applications, Go has a huge advantage in terms of compiling to a static binary and performance. We can use Go to build super-fast server side applications that are scalable and resilient.

Go rules the Cloud Native world (another motivation for me personally to learn Go). Kubernetes, Docker and Prometheus are all written in Go.

Installing Go on our machine

Before we can write any Go, we'll need to install the tools to do so. We can install these by heading to the Go website. We can install it from a package (.msi for Windows, .pkg for Mac and .tar.gz for Linux) or we can compile it from source.

I went the package route, which isn't as cool I know, but that will automatically install Go in the correct locations, remove any old installations of Go you may have and more importantly, put the Go binary in the default executable path.

For windows users, you can install Go using Chocolatey like so:

choco install golang
Enter fullscreen mode Exit fullscreen mode

For Mac users, you can use Homebrew:

brew install golang
Enter fullscreen mode Exit fullscreen mode

We can validate that Go has been installed correctly by opening up a command prompt or terminal and running the following command:

go version
Enter fullscreen mode Exit fullscreen mode

If everything was setup correctly, we should see the following output:

go version go1.14.4 windows/amd64
Enter fullscreen mode Exit fullscreen mode

What this command tells me is that I'm running Go version 1.14.4 on Windows. Your Go version may be different and if you're using a Mac or Linux, you won't see Windows in your output.

Using Go, we will need to install 3rd party tools, which can be installed using the go install command. By default, these tools will be installed in the $HOME/go location. The source code for these tools will be situated in the $HOME/go/src directory and the compiled binaries will be stored in the $HOME/go/bin directory.

It's a good idea to put our $HOME/go/bin directory on our executable path, which makes it clear where our Go workspace is situated and makes it easier to run 3rd party tools. On Windows, we can do this by running the following:

setx GOPATH %USERPROFILE%\go
setx path "%path%;%USERPROFILE%\bin"
Enter fullscreen mode Exit fullscreen mode

Installing the Go extension on VS Code

For these tutorials, I'll be using Visual Studio Code for my developer environment. It's free and has a fantastic extension that we can use to write Go code.

If you haven't got VS Code, you can download it here.

Once you've downloaded VS Code, open it up and install the Go extension by clicking on the Extensions icon in the left hand sidebar, and then typing in Go in the text box. Pick the Go extension developed by the Go Team at Google and click Install.

Go VS Code extension

Now that this has been installed, let's take everything for a spin and create our first Hello World app in Go!

Building and Running a basic Hello World app!

In VS Code, create a new folder and in that folder, create a new file called main.go. In that Go file, write the following:

package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
}
Enter fullscreen mode Exit fullscreen mode

Let's break this file down:

  • Every Go program is made up of packages. We declare our package by writing package main. Programs in Go start running in the man package.
  • We then import the fmt library in the line import "fmt". We can import multiple packages into our Go code if we use multiple packages. The fmt package implements formatted I/O functions that we can use in our program, without having to write these functions ourselves. We can leverage libraries in our Go code by importing them.
  • We then create our entry function by writing func main(). This is our main entry point of our program.
  • We then print out our "Hello, World!" statement by writing fmt.Println("Hello, World!"). The Println() function is a inbuilt function of the fmt package which we can use to format our input and write it to standard output.

There are a couple of ways we can run this program. Let's start by running the file like so:

go run main.go
Enter fullscreen mode Exit fullscreen mode

With this command, we'll see the following output:

Hello, World!
Enter fullscreen mode Exit fullscreen mode

What the go run command does is compile our Go code into a binary, executes it and then deletes the binary. It builds the binary into a temporary directory. This is useful when we want to test out small Go programs or treat our Go code like a script.

If we want to build a binary and keep it, we can run the following.

go build main.go
Enter fullscreen mode Exit fullscreen mode

What this command does is create an executable for us called main.exe (or just main if you're on Mac) in the directory where our Go file is. To run this, we just call the executable and it should print out Hello, World! again. If we want to give our executable a name, we can define that name like so:

go build -o hello_world main.go
Enter fullscreen mode Exit fullscreen mode

Conclusion

In this article, I showed you how you can install Go onto your machine and how you can setup Visual Studio Code to work with Go. We also wrote our first bit of Go code, built a binary from it and executed it! While this program's not going to make you the next tech unicorn, this was an important first step!

I've kept this first step simple just to get our developer environment going. Once we have this setup, we can start to explore Go a little bit more. If you're confident, I'd recommend checking out the following resources if you want to explore Go on your own:

If you have any questions or had trouble getting this all set up, feel free to reach out to me on twitter @willvelida and I'll try and help out the best I can.

In my next post, we'll take another small step into the world of Go and discuss variables and types.

Until next time, Happy coding! 🤓🖥️

Top comments (2)

Collapse
 
netdesign8 profile image
Don McC

Hello Will,
Thanks for this post - it was useful...
I wanted to ask you if the (second) line in this post was what you intended ?

setx GOPATH %USERPROFILE%\go
setx path "%path%;%USERPROFILE%\bin"
Enter fullscreen mode Exit fullscreen mode

I think what you intended to do in the 2nd cmd was this:

setx path "%path%;%GOPATH%\bin"
Enter fullscreen mode Exit fullscreen mode