Skip to content

Hello, Go!

The Course
•
18 min study

Go is a compiled language. When you press Run, the editor first turns your source code into a program the computer can execute, then starts that program. If the compiler finds a mistake, nothing runs at all: you get an error message instead of partial output. That can feel strict at first, but it means many mistakes are caught before your program ever does anything.

Every runnable Go program has the same three parts. It starts with a package declaration, and a program you can run always uses package main. Next comes an import list naming the packages the file uses; here that is fmt, the standard package for printing text. Last is func main, the function where the program starts. When main finishes, the program ends.

The first Run downloads about 21 MB of compiler files, so it can take a while. After that it is much faster. Each Run starts your program again from the top, so nothing carries over from the previous run.

Predict and run

Which line appears first?

Predict, then run Go
package main

import (
	"fmt"
)

func main() {
	fmt.Println("Hello, Go!")
	fmt.Println("Ready to read")
}
Check your prediction
Text
Hello, Go!
Ready to read

Change the greeting. Try removing a closing quote, read the compiler message, then restore it.

Statements run in order

Inside main, each line is a statement, and Go runs them from top to bottom. fmt.Println prints whatever you pass it and then ends the line, so the next call starts on a fresh line. Called with nothing between the parentheses, it prints an empty line, which is a simple way to leave a gap in your output.

The name fmt.Println has two parts. fmt is the package, and Println is a function inside it. The capital P matters: Go is case-sensitive, so fmt.println is a different name that does not exist.

Order and blank lines Go
package main

import (
	"fmt"
)

func main() {
	fmt.Println("Reading log")
	fmt.Println()
	fmt.Println("Poems")
	fmt.Println("Short stories")
}
Text
Reading log

Poems
Short stories

Comments explain, the compiler ignores

Text after // up to the end of the line is a comment. Go skips it completely, so comments are for people reading the code: you next week, or a teammate. Use them to explain why a line is there, not to repeat what it obviously does. You can also put // in front of a statement to switch it off for a moment while you experiment.

Comments do not print Go
package main

import (
	"fmt"
)

func main() {
	// The report title comes first.
	fmt.Println("Reading log")
	// fmt.Println("This line is switched off")
	fmt.Println("Start here") // A comment can follow code too.
}
Text
Reading log
Start here
Go has one layout

Go code is formatted by a tool called gofmt, so almost every Go file you read looks the same: tabs for indentation, and the opening brace of a function on the same line as func main(). Moving that brace to its own line is not just a style choice in Go; it is a compile error. If you follow the layout of the examples, you will rarely hit it.

Reading a compiler message

When something is wrong, the message names a line and a column, such as ./main.go:8:14, followed by a short description. Read the first message first: later messages are often side effects of the first mistake. Common ones on Day 1 are a missing closing quote (newline in string), a missing import (undefined: fmt), and an import you no longer use ("fmt" imported and not used). Go treats an unused import as an error so that files do not collect clutter.

Practice

Exercise

Your turn

The starter prints a draft line that should not be in the report. Switch it off with a comment, keeping the line in the file, and add a blank line and then Week 1. The output should be:

Text
Reading log

Week 1
Your working space Go
package main

import (
	"fmt"
)

func main() {
	fmt.Println("Reading log")
	fmt.Println("DRAFT: delete before printing")
}
Hint 1: the idea

The compiler skips everything after // on a line, and fmt.Println() with nothing inside prints an empty line.

Hint 2: a first step

Put // at the start of the draft line and run it. Only Reading log should print. Then add the two new lines below it.

Worked solution
Run the solution Go
package main

import (
	"fmt"
)

func main() {
	fmt.Println("Reading log")
	// fmt.Println("DRAFT: delete before printing")
	fmt.Println()
	fmt.Println("Week 1")
}
Text
Reading log

Week 1

The draft line is still there for whoever reads the code, but the compiler ignores it. fmt.Println() prints only the line ending, which is the empty line between the title and Week 1.

Was this article helpful?

Your response is saved on this device.