Kodewick
Start learning →
Lesson 1 of 24Contents

Hello, World!

Lesson 1 of 24 · Part 1 · Getting started · about 15 minutes

Your first Go program does one thing: it shows a line of text on the screen. Every piece of code on this page runs right in your browser: press Run, change something, and run it again.

prog.go
1package main2 3import "fmt"4 5func main() {6	fmt.Println("Hello, World!")7}
Output
Hello, World!

Here is what each part does:

PartWhat it is
package mainEvery Go file belongs to a package. A program you run is package main.
import "fmt"Brings in the fmt package, which has the tools for printing.
func main()The program starts running at main.
{ }The curly braces hold what main does.
fmt.Println("...")Shows the text in quotes, then starts a new line.

There is no semicolon at the end of the line: Go adds them for you.

Line by line

The statements in main run from top to bottom, one after another. Press Step to watch it go line by line.

prog.go
1package main2 3import "fmt"4 5func main() {6	fmt.Println("First line")7	fmt.Println("Second line")8	fmt.Println("Third line")9}
Output
First line
Second line
Third line

Println and Print

fmt.Println can show several things at once: put commas between them and it puts a space between each, numbers included. fmt.Print shows them without starting a new line at the end:

prog.go
1package main2 3import "fmt"4 5func main() {6	fmt.Println("Apples:", 3)7	fmt.Println("Total:", 3+4)8	fmt.Print("No new line, ")9	fmt.Print("so this comes right after\n")10}
Output
Apples: 3
Total: 7
No new line, so this comes right after

\n inside quotes is a new line, so Print("…\n") ends the line just as Println does.

Guess first

How many lines does this show?

1package main2 3import "fmt"4 5func main() {6	fmt.Print("A")7	fmt.Println("B")8	fmt.Println("C")9}

What compiling is

Go is a compiled language: your code first goes to the compiler (go build), which turns it into a program the computer can run. Only then does it run. Here, when you press Run, the page does both for you, with the same results as Go 1.24.

The compiler is strict. Import a package and never use it, and it refuses to build:

prog.go
1package main2 3import (4	"fmt"5	"strings"6)7 8func main() {9	fmt.Println("Hello")10}
Output
# command-line-arguments
./prog.go:5:2: "strings" imported and not used

Read the line with the file name: ./prog.go:5:2 is the file, line 5, character 2, and after it comes what is wrong. Unused packages and unused variables are errors in Go, not warnings: this keeps programs clean. Delete the "strings" line and run it again.

Braces on the same line

In Go the opening brace { must be on the same line as func main(). Put it on the next line and the program doesn't compile:

prog.go
1package main2 3import "fmt"4 5func main()6{7	fmt.Println("Hello")8}
Output
# command-line-arguments
./prog.go:6:1: syntax error: unexpected semicolon or newline before {

Go added a semicolon at the end of func main(), so it sees a function with no body. Every Go program is laid out the same way; the gofmt tool even rewrites your code into this layout, with tabs for indentation.

Comments

Comments are for people; the compiler skips them. From // to the end of the line is a comment, and so is everything between /* and */.

prog.go
1package main2 3import "fmt"4 5func main() {6	// say hello first7	fmt.Println("Hello")8	/* not this one for now:9	fmt.Println("Goodbye")10	*/11}
Output
Hello

Practice

Write the code, then press Check. A hint costs nothing; the answer is there when you need it.

Exercise 1

Your first program

Make the program show Hello, Go!, then a new line. (The starting code doesn't compile yet: fmt is imported but not used.)

It should print
Hello, Go!
exercise1.go
1package main2 3import "fmt"4 5func main() {6 7}
Output
Press Run or Ctrl+Enter
Exercise 2

Several things in one Println

Using one fmt.Println, show Price: 25 Count: 4 (the numbers as numbers, not inside the quotes).

It should print
Price: 25 Count: 4
exercise2.go
1package main2 3import "fmt"4 5func main() {6	fmt.Println("Price:")7}
Output
Press Run or Ctrl+Enter
Exercise 3

Fix it: an import nobody uses

This program doesn't compile. Fix it so it shows Ready.

It should print
Ready
exercise3.go
1package main2 3import (4	"fmt"5	"strings"6)7 8func main() {9	fmt.Println("Ready")10}
Output
Press Run or Ctrl+Enter

What you learned

  • A Go program is package main with a func main(); it starts running at main.
  • import "fmt" brings in fmt.Println and fmt.Print; Println puts spaces between its values and ends the line.
  • Go adds the semicolons, and the { stays on the same line.
  • An unused package or variable is a compile error; the error gives the file, line and character.
  • // and /* */ are comments.
Finished this lesson?Mark it. Progress is saved in this browser, and you can undo it.