Your First Program

Traditionally the first program you write in any programming language is called a “Hello World” program – a program that simply outputs Hello World to your terminal. Let's write one using Go.

First create a new folder where we can store our program. Create a folder named ~/src/golang-book/chapter2. (Where ~ means your home directory) From the terminal you can do this by entering the following commands:

mkdir src/golang-book
mkdir src/golang-book/chapter2

Using your text editor type in the following:

package main

import "fmt"

// this is a comment

func main() {
    fmt.Println("Hello World")

Make sure your file is identical to what is shown here and save it as main.go in the folder we just created. Open up a new terminal and type in the following:

cd src/golang-book/chapter2
go run main.go

You should see Hello World displayed in your terminal. The go run command takes the subsequent files (separated by spaces), compiles them into an executable saved in a temporary directory and then runs the program. If you didn't see Hello World displayed you may have made a mistake when typing in the program. The Go compiler will give you hints about where the mistake lies. Like most compilers, the Go compiler is extremely pedantic and has no tolerance for mistakes.

How to Read a Go Program

Let's look at this program in more detail. Go programs are read top to bottom, left to right. (like a book) The first line says this:

package main

This is known as a “package declaration”. Every Go program must start with a package declaration. Packages are Go's way of organizing and reusing code. There are two types of Go programs: executables and libraries. Executable applications are the kinds of programs that we can run directly from the terminal. (in Windows they end with .exe) Libraries are collections of code that we package together so that we can use them in other programs. We will explore libraries in more detail later, for now just make sure to include this line in any program you write.

The next line is a blank line. Computers represent newlines with a special character (or several characters). Newlines, spaces and tabs are known as whitespace (because you can't see them). Go mostly doesn't care about whitespace, we use it to make programs easier to read. (You could remove this line and the program would behave in exactly the same way)

Then we see this:

import "fmt"

The import keyword is how we include code from other packages to use with our program. The fmt package (shorthand for format) implements formatting for input and output. Given what we just learned about packages what do you think the fmt package's files would contain at the top of them?

Notice that fmt above is surrounded by double quotes. The use of double quotes like this is known as a “string literal” which is a type of “expression”. In Go strings represent a sequence of characters (letters, numbers, symbols, …) of a definite length. Strings are described in more detail in the next chapter, but for now the important thing to keep in mind is that an opening " character must eventually be followed by another " character and anything in between the two is included in the string. (The " character itself is not part of the string)

The line that starts with // is known as a comment. Comments are ignored by the Go compiler and are there for your own sake (or whoever picks up the source code for your program). Go supports two different styles of comments: // comments in which all the text between the // and the end of the line is part of the comment and /* */ comments where everything between the *s is part of the comment. (And may include multiple lines)

After this you see a function declaration:

func main() {
    fmt.Println("Hello World")

Functions are the building blocks of a Go program. They have inputs, outputs and a series of steps called statements which are executed in order. All functions start with the keyword func followed by the name of the function (main in this case), a list of zero or more “parameters” surrounded by parentheses, an optional return type and a “body” which is surrounded by curly braces. This function has no parameters, doesn't return anything and has only one statement. The name main is special because it's the function that gets called when you execute the program.

The final piece of our program is this line:

    fmt.Println("Hello World")

This statement is made of three components. First we access another function inside of the fmt package called Println (that's the fmt.Println piece, Println means Print Line). Then we create a new string that contains Hello World and invoke (also known as call or execute) that function with the string as the first and only argument.

At this point we've already seen a lot of new terminology and you may be a bit overwhelmed. Sometimes its helpful to deliberately read your program out loud. One reading of the program we just wrote might go like this:

Create a new executable program, which references the fmt library and contains one function called main. That function takes no arguments, doesn't return anything and does the following: Access the Println function contained inside of the fmt package and invoke it using one argument – the string Hello World.

The Println function does the real work in this program. You can find out more about it by typing the following in your terminal:

godoc fmt Println

Among other things you should see this:

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.

Go is a very well documented programming language but this documentation can be difficult to understand unless you are already familiar with programming languages. Nevertheless the godoc command is extremely useful and a good place to start whenever you have a question.

Back to the function at hand, this documentation is telling you that the Println function will send whatever you give to it to standard output – a name for the output of the terminal you are working in. This function is what causes Hello World to be displayed.

In the next chapter we will explore how Go stores and represents things like Hello World by learning about types.


← PreviousIndexNext →