Learn/Go/1. Hello, output
Lesson 1 of 15
Sign in

Go, lesson 1

Hello, output

Every Go program starts from a file that belongs to package main and has a function called main. When you run the program, Go calls main and runs its statements from top to bottom. When main returns, the program ends.

Three parts make this work:

  • package main tells Go that this file builds a program you can run, not a library.
  • import "fmt" loads the standard format package, which knows how to print.
  • func main() { ... } is where your program begins. The opening brace must sit on the same line as func main(); Go's grammar depends on it.

Go is strict about imports: if you import a package and never use it, the program does not compile. That feels fussy at first, but it keeps programs tidy.

2 exercises to try what you just read.

Exercise 1

Your first program

Press Ctrl Enter or Check to test your code.

Exercise 2

A shop receipt

Press Ctrl Enter or Check to test your code.

Three ways to print

The fmt package has three printing functions you will use constantly.

fmt.Println prints its values separated by spaces and ends the line:

fmt.Print does not add a newline at the end, and only adds spaces between two values when neither of them is a string. Use it when you want to build one line from several calls.

fmt.Printf takes a format string with placeholders called verbs. Each verb is replaced by the next value. %s is for strings, %d for whole numbers, %v for any value in its default form, and %% prints a literal percent sign. Printf never adds a newline for you, so end the format with \n.

The \n inside a string is an escape sequence that means "new line". Another useful one is \t for a tab.

Comments

Go has two kinds of comments. // starts a comment that runs to the end of the line. /* ... */ can wrap several lines. The compiler ignores both.

How a program runs

Go is a compiled language. Before anything runs, the compiler checks the whole file and turns it into a machine-code program. If there is a mistake, such as a missing quote or an unused import, you get an error with a file name and line number, and nothing runs at all. Once it compiles, main runs each statement in order.

Further reading

  • A Tour of Go: Packages, imports and functions
  • Package fmt
  • How to Write Go Code

Write a program that prints exactly these two lines:

The program reads no input.

Print this small receipt. Use fmt.Printf with %s and %d for the two item lines, and remember that %% prints a percent sign.

The program reads no input.

package main

import "fmt"

func main() {
	fmt.Println("Hello, World!")
	fmt.Println("Go runs main from top to bottom.")
}
fmt.Println("Score:", 42)      // Score: 42
fmt.Println(3, "lives left")   // 3 lives left
fmt.Print("Loading")
fmt.Print("...")
fmt.Print("done\n")            // Loading...done
fmt.Printf("%s scored %d points\n", "Ada", 95)   // Ada scored 95 points
fmt.Printf("Battery: %d%%\n", 80)                // Battery: 80%
// This explains the next line.
fmt.Println("This runs") // a comment can follow code

/*
   A longer note can go here.
*/
Hello, World!
I am learning Go.
Item: apples x 3
Item: bread x 1
Discount: 10%