Golang – A ‘hello world’ example

Once you’ve installed Golang and set up your workspace, you’re now ready to write your hello world go program. Your Go code has to be written in the src folder, and the file needs to have the ‘.go’ extension. So I created the following file:


$ cat hello.go
package main

// this is a comment. 
import "fmt"


func main() {
    fmt.Println("hello world")
} 

There are 2 types of Go projects you can create. One is a shared library project which is code that’s used as part of other Golang projects. The other is where the end product of a Golang project is a standalone executable binary.

The package main tells Golang that this project will be creating a standalone executable binary. Next, we declared a function called ‘main’. You can choose what to call your function, however naming a function ‘main’ has special meaning in Golang, it tells Golang what is the first block of code to execute when the binary gets called. The binary exits out once the code in the main function finishes executing.

Golang comes builtin with a collection of packages. This collection is known as the standard library.

In our example, we’ve called another function called ‘Println‘. Println is basically a function that print’s content to the standard output. This function is defined inside the fmt package. fmt is one of the standard library packages. The import declaration is how we load in the fmt package so that we can call its function in our code.

Now we can test our code by running:

$ go run hello.go
hello world

This ‘run’ command performed a number of tasks behind the scenes. First, it created a binary from our code, executed that binary, then deleted that binary.

Now that has worked, let’s now create the binary. First we need to set the GOBIN variable which tells Golang where to store the binary end product to:

$ export GOBIN="$GOPATH/bin"

Then while in the src folder, run the install command:

$ go install hello.go

This ends up compiling the binary and then saving it to the GOBIN folder:

$ echo $GOBIN
/root/go_project/bin
$ ll $GOBIN
total 1968
-rwxr-xr-x. 1 root root 2011612 Aug 12 01:30 hello
$ file /root/go_project/bin/hello
/root/go_project/bin/hello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped

We can now run the standalone binary:

$ /root/go_project/bin/hello
hello world

We had to specify the full path of the binary, but if you want to run this binary like any other command then just move it to one of the folders specified in the PATH environment variable.