Hello World in Go

3 minute read

This is a step-by-step guide to running your first Go application. It assumes that you already have installed the Go compiler. Setting up a new Go project may be a little more difficult than in some other languages, but this tutorial will help you start in no time.

This tutorial uses Go v1.16 but should work with any reasonably recent version.

Make sure your environment is set up correctly

If you have installed Go correctly, you should have the $GOPATH variable set on your machine. To make sure that you have, open your terminal/console application and type: echo $GOPATH. It should print something similar /Users/coding/go.

If you don’t have it set, you can quickly set it by typing export GOPATH=<your path>. Setting that path is critical because it will point to where your packages and dependencies are located.

Create a new file in your Go working directory

Now, let’s create source code file. Let’s assume your $GOPATH is set to /Users/coding/go.

First, create directories where your project will be located.

Execute the following line in your terminal: it will create a project for your app. It assumes that you will host it on Github. Remember to change <username> to your own username on Github.

1mkdir -p $GOPATH/src/github.com/<username>/myapp

In the new myapp directory, create a new file called hello_world.go. Insert the following Go code:

1package main
2
3import "fmt"
4
5func main() {
6    fmt.Println("Hello, World!")
7}
8

What’s happening in this simple program?

This program has only 5 lines. Let’s go through what’s happening there.

  • package main

A package groups a number of different files. It can be either a library (reusable piece of code) or an executable (a program you can run). A package named main (like here) is special: it defines an executable file.

  • import "fmt"

Here, we’re letting the compiler know we’re going to be using the fmt package. It has a number of useful functions, such as Println which we use to print a string to the standard output (e.g. terminal).

  • func main() {

We define the main() function. main() function is special, that’s where the execution of the program starts. Body of the function is enclosed in curly braces ({ ... }). Be careful: Go cares how they’re laid out, i.e. the opening curly brace needs to be on the same line as the function name.

  • fmt.Println("Hello, World!")

Println function prints a single line of text. It’s in the fmt package we had imported previously.

Then, we just close the body of the function (}) and that’s it!

Executing your first application

It’s a very simple program that prints “Hello, World” to your console. To run it, make sure you’re in the myapp directory and execute: go run hello_world.go. You should see a message on the screen.

Building your “hello world” program

To build the program, we first need to create program dependencies file (even though we don’t have any except for the standard library). To do that simply type:

1go mod init

After that, you should see go.mod file in your app directory.

Then, simply type:

1go build

You should get a binary file named after your directory (myapp). You can run it like usual: ./myapp. In order to get a binary executable with a custom name, run go build -o <mycustomname> (change <mycustomname> as required!).

Use this simple setup to develop your application into something more advanced.