Getting Started with Go

In order to get started with Go, it must first be installed. The process for installation varies depending on the operating system in use. Further details can be found here.

A Go program can be written using any text editor, however, a code editor, or Integrated Development Environment, that supports Go and includes syntax highlighting can be helpful.

Code Commenting

When programming in any language it is useful to add comments to the code to describe what is happening and why. This is useful when the code is revisited at a later date to make enhancements, or for debugging purposes. In order to add a comment in Go it needs to start with ‘//’, as shown below.

// This is a comment in Go.

It is also possible to have comments that span multiple lines. Multi-line comments must start with ‘/*’ and end with ‘*/’.

/* This is 
a multi-line
comment
in Go.*/

Packages, Modules and Repositories in Go

Programs written in Go are organised into packages, which consist of a collection source code files contained in the same directory and are compiled together. Source code files have a '.go' file extension, and each file within a package is visible to one another and can utilise the code within. A module is a collection of packages that are released together. A file called 'go.mod' contains the import path prefix for any packages within the module.

Even if there is no immediate desire to publish code to a remote repository, such as GitHub, it is a good idea to organise code for this purpose in case it is needed at some point.

First Go Program

First of all, create a folder called, for example, 'demo', where the program will reside. Once this is done, navigate to the folder in a console or terminal. Next, a module needs to be created, specifying the path. This can be done using the following command and will create the 'go.mod' file.

go mod init example/demo

Here, 'example/demo' is the import path prefix for any packages within the module. Once this is done, create a file in the folder with a '.go' file extension, such as 'demo.go'. The code written below can be placed within this file. This just displays the words ‘Hello World!’ in a console or terminal window.

// Package name.
package main

// Packages to import.
import (
    "fmt"
)

// The 'main' function.
func main() {

    // Display a message in the console/terminal.
    fmt.Println("Hello World!")

}

The package name of 'main' indicates to the compiler that the package should be compiled as an executable program, rather than a shared library. The 'import' statement is used to include any packages that are required in the program. All executable programs need to contain a 'main' function which indicates the entry point of the program. Finally, the 'Println' function, within the 'fmt' package is used to display the message 'Hello World!'

Running a Go Program

Running the above program can be done as follows from within a console or terminal.

go run .

As an alternative, the file name can be explicitly specified.

go run demo.go

Both methods will produce the same result.

Hello World!