Program Structure

Here is the program from the last lesson again. This time, let's look at each line.

package main

import "fmt"

func main() {
	fmt.Println("Hello, Go!")
}

Output:

Hello, Go!

package main Go code is organized in packages. A package is a group of Go files that belong together. The name main is special. It tells Go that this package is a program you can run. Every program in this course starts with this line.

import "fmt" brings in the fmt package. It is part of Go's standard library, the set of packages that comes with Go. fmt (short for "format") has the functions that print text and read input. Without this line, Go does not know what fmt means.

func main() { ... } func starts a function, a named block of code. The function named main is where the program starts. Go runs the lines inside the curly braces { } from top to bottom, and when it reaches the closing }, the program ends.

fmt.Println("Hello, Go!") uses the Println function from the fmt package. Text in double quotes is called a string. Println prints it and then moves to a new line ("ln" means "line"). You do not need a semicolon at the end of the line.

Printing several values

You can give Println several values, with commas between them. It prints them on one line, with a space between each pair:

package main

import "fmt"

func main() {
	fmt.Println("Line one")
	fmt.Println("Line", 2)
}

Output:

Line one
Line 2

Shorter examples from now on

To keep the lessons short, many examples from now on show only the lines that go inside main. When you see one, imagine it inside func main() { ... }, with package main and the imports on top. The exercises always show the whole program.

Go is strict

Go cares about small details, and the compiler tells you when one is wrong. Upper and lower case letters are different in Go, so println and Println are two different names:

package main

import "fmt"

func main() {
	fmt.println("Hello, Go!")
}

Output:

# command-line-arguments
./main.go:6:6: undefined: fmt.println

You can skip the first line of the error. The second line is the useful one. ./main.go:6:6 means file main.go, line 6, character 6. undefined means Go has never heard of that name. The functions you use from another package always start with a capital letter.

The opening { must also be on the same line as func main(). Here it is on the next line:

package main

import "fmt"

func main()
{
	fmt.Println("Hello, Go!")
}

Output:

# command-line-arguments
./main.go:6:1: syntax error: unexpected semicolon or newline before {

Last, Go does not allow an import that you never use. This program imports os (a package for working with the operating system) but never uses it:

package main

import (
	"fmt"
	"os"
)

func main() {
	fmt.Println("Hello, Go!")
}

Output:

# command-line-arguments
./main.go:5:2: "os" imported and not used

The parentheses let one import bring in several packages. The program does not run at all, even though the Println line is fine. To fix it, delete the import you do not need. This rule keeps programs free of leftovers.

In the exercise, a program is missing its package name, its import and the name of its function. Fill them in.