Writing your first `Hello World` in GoLang

jonthanfielding

Jonathan Fielding

Posted on March 3, 2021

Writing your first `Hello World` in GoLang

Recently I was given the feedback that I needed to broaden my skillset when it comes to the technologies I use to enable me to progress further in my career as an Engineer.

As a more ‘frontend focused’ full-stack engineer, the backend languages I have had experience with so far are those more commonly picked up by frontend engineers, that being PHP early on in my career and then later moving to Node.js.

I decided to pick Go Lang as a technology RVU already uses as the programming language to learn. The benefit is that I would immediately have use for it in the workplace, giving me a chance to apply what I have been learning.

With all this in mind, I will try to document my learning here to help others who are trying to learn Go Lang as well.

Installing Go Lang

Before we can get started with Go Lang, we first need to set up our development environment. As a Mac user, I will document how to do this on Mac using the command line (Terminal) however, Windows instructions can be found at https://golangdocs.com/install-go-windows.

Install Homebrew

The first step is to install homebrew if you do not already have it installed. Homebrew is a package manager for macOS that allows you to install different command-line tools and programming languages onto your computer.

Installing homebrew is fairly straight forward; we simply run the following command:

/bin/bash -c$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Enter fullscreen mode Exit fullscreen mode

We will then be asked for our password, and the install script will then do all the hard work for us. If you get stuck, the documentation can be found at https://brew.sh.

Configure your system paths

Having installed homebrew we now need to configure some system paths.

The system paths will need to be added to your systems shell configs. The file you will need to edit will differ based on the Shell your command-line interface is using. To find out what Shell we are using, we can run echo $SHELL in our CLI; this will tell us the path to the Shell we are currently using.

If the output is /bin/zsh or similar, you are using zsh and will need to edit the .zshrc file located in your users’ home directory.

If the output is /bin/bash or similar, you are using bash and will need to edit the .bashrc file located in your users’ home directory.

You will need to copy and paste the following paths to your Shell config file.

# Go development
export GOPATH=”${HOME}/.go”
export GOROOT=”$(brew — prefix golang)/libexec”
export PATH=”$PATH:${GOPATH}/bin:${GOROOT}/bin”
test -d “${GOPATH}” || mkdir “${GOPATH}”
test -d “${GOPATH}/src/github.com” || mkdir -p “${GOPATH}/src/github.com”
Enter fullscreen mode Exit fullscreen mode

Install Go Lang using homebrew

Finally, we can install Go Lang, to do this we simply run the following command:

brew install go
Enter fullscreen mode Exit fullscreen mode

This will automatically install Go for us on your computer, once this is done we are ready to setup our text editor and then write our first app 😀

Configuring Visual Studio Code

As a heavy user of Microsoft’s Visual Studio Code (aka VS Code), the next step before writing any code is to install some plugins to add Go Lang support. If you are not a VS Code user, please feel free to skip this step.

As this is my first day using Go Lang I don’t have any life-changing suggestions for extensions right now however the official plugin for VS Code by the Go Team at Google seems pretty good. It can be found at https://marketplace.visualstudio.com/items?itemName=golang.Go.

Writing your first hello world

Having set up my environment I can finally write my first code in Go Lang, as is customary in the tech space this will be a hello world application that simply logs ‘hello world’ to the console.

To start with we need to create a file for our hello world application, we will call it hello-world.go. We will open this in our text editor and then write out code, for our ‘hello world’ application, the code looks like below.

package main

import "fmt"

func main() {
    fmt.Println("hello world")
}
Enter fullscreen mode Exit fullscreen mode

As you can see we imported the Go Lang library fmt and then used that to simply log to the console using fmt.Println("hello world").

We can run our code by running the following in our CLI from the folder that we are using.

go run hello-world.go
Enter fullscreen mode Exit fullscreen mode

What’s next?

Today I managed to get my environment setup and write my first application😁. It was super fun getting this all setup and hopefully, this can be helpful to others trying to learn Go Lang for the first time.

💖 💪 🙅 🚩
jonthanfielding
Jonathan Fielding

Posted on March 3, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related