topic logo
Go ⇢ Unit 1

Hello World!

getting started..

Summary

Introduces a simple application and explains which parts it consists of and how to build it combined with some Go lore.

Background

In good tradition this tutorial starts with the “Hello World!” program that prints this string to the output. The purpose of this unit is to get started with the basic structure of a program and the means needed to compile and run it.

Before you start with this unit, you’ll need to download the Go tool-set from golang.org/dl. You’ll also need a capable text editor, preferably with Go syntax highlighting and a terminal emulator that is shipped with most desktops.

This tutorial will list terminal commands for the “bash” shell, which is a widely used shell on Linux and Mac. It’s also ported to Windows and other systems. For beginners wishing to follow the examples here, I suggest to use the “bash” shell. There is no reason that prevents the usage of other shells, though these have different syntaxes.

New Functions

New types and functions are introduced at the beginning of each unit in a listing as seen below.

  • fmt.Println(a ...interface{}) (n int, err error)

    Println formats using the default formats for its operands and writes to standard output. Spaces are always added between operands and a newline is appended. It returns the number of bytes written and any write error encountered.

“Hello World!” Application

The example application can be downloaded as compressed archive from the header of this page, from the link “Download Unit Source Code”. This is true for all units in this tutorial.

hello.go - "Hello World!" program
package main

import (
    "fmt"
)

func main() {
    fmt.Println("Hello World!")
    fmt.Println("こんにちは世界")
}

The above example application prints the hello message twice, the second time in Japanese to demonstrate that Go programs are encoded utf-8 by specification and make working with non-ASCII characters natural. (Note: if the second Println argument does not show up properly for you, then your system is missing a Japanese font.)

package main

Each Go source file starts with the package directive, that tells which package it belongs to. Packages producing an executable are always named “main” and have a function with the same name in one of the source files.

import (
    "fmt"
)

The “fmt” package is part of the standard library and provides functions to read and write text on the terminal. The import statement makes all of the package’s public data types and functions available.

Important

Public types and functions are identified by a capital starting letter.

func main() {
    fmt.Println("Hello World!")
    fmt.Println("こんにちは世界")
}

The main function is the entry point of the executable when it’s run. Here it calls the “Println” function from the “fmt” package twice. First with the English version of “Hello World!” and then with the Japanese equivalent to illustrate how friction-less it is to work with international characters.

Building and Running

There are basically two ways to build and execute this application: in-place or using a Go workspace.

In-Place Build and Run

% go run hello.go

Terminal shells can be configured. I like to use a percent sign as prompt, so when this tutorial shows some code starting with a percentage sign, it indicates a terminal command.

Here I run the “go” command with the “run” argument followed by the source files that I want to run. This needs to be done in the folder where the source file is located.

This command compiles and immediately executes the application. It does not keep a permanent binary behind, so each time it’s called, it compiles the program. For smaller programs this is not an issue though, because compilation times for Go programs are very low.

To do this with the example code in this tutorial, you’ll need to download and extract the compressed archive in the header of this site, then open the terminal, navigate to the location of the source file and then enter the above command.

the output
% go run hello.go
Hello World!
こんにちは世界

Go Workspace

When not specifying specific paths, the “go” tool works on a workspace. This is a file system structure that is rooted at the path the $GOPATH environment variable points to.

bash.rc - setting the GOPATH variable
export GOPATH=/home/seth/Go

The above path shows how to add a path to this environment variable in the “bash.rc” file, which is sourced each time a new terminal is launched.

skeleton of a Go workspace
$GOPATH
  + src
  + bin
  + pkg

The $GOPATH directory must contain three sub-directories containing the Go source code files, the executables and the object files for library packages.

location of the hello.go file in a workspace
$GOPATH/src/github.com/skriticos/hello/hello.go

Go packages are imported with a path that should be unique in the Go ecosystem. The usual method to do this is to map a URL base path in the source directory that you control, like a github.com user path. Then put all packages in this path. This ensures that your packages don’t collide with other people’s packages that share the same name.

Hint

Mapping packages to a URL does not mean that you have to push or publish it there. But if you do, go provides a few commands to quickly install them form the URL.

compile and store the application binary
% go install github.com/skriticos/hello

The above command can be called from anywhere in the filesystem with the same result. It will look up the “hello” package in the workspace, compiles it and saves the binary.

bash.rc - execute your applications from anywhere
export PATH=$PATH:$GOPATH/bin

The above statement in the bash user configuration file allows to execute your binaries from any path in the filesystem.

% hello
Hello World!
こんにちは世界

When you installed the “hello” application, you can simply execute it with it’s name.

Getting Help

read reference documentation
% godoc fmt
...
% godoc fmt Println
...

The two “godoc” commands above print the reference documentation about a subject. The first one shows general information about the “fmt” standard library package. The second shows the signature and description of the “Println” function that is part of the “fmt” package.

Conclusion

This unit explained the basics to get started with the Go programming language and the Go workspace and how to compile and execute applications written in Go. These essential concepts are used in the following units.