Hello, World!
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.
1package main2 3import "fmt"4 5func main() {6 fmt.Println("Hello, World!")7}
Hello, World!
Here is what each part does:
| Part | What it is |
|---|---|
package main | Every 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.
1package main2 3import "fmt"4 5func main() {6 fmt.Println("First line")7 fmt.Println("Second line")8 fmt.Println("Third line")9}
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:
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}
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.
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}
fmt.Print("A") doesn't start a new line, so B comes right after A: AB, then Println ends the line. Then C gets a line of its own. Two lines in all.
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:
1package main2 3import (4 "fmt"5 "strings"6)7 8func main() {9 fmt.Println("Hello")10}
# command-line-arguments
./prog.go:5:2: "strings" imported and not usedRead 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:
1package main2 3import "fmt"4 5func main()6{7 fmt.Println("Hello")8}
# 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 */.
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}
Hello
Practice
Write the code, then press Check. A hint costs nothing; the answer is there when you need it.
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.)
Hello, Go!
1package main2 3import "fmt"4 5func main() {6 7}
Several things in one Println
✓Using one fmt.Println, show Price: 25 Count: 4 (the numbers as numbers, not inside the quotes).
Price: 25 Count: 4
1package main2 3import "fmt"4 5func main() {6 fmt.Println("Price:")7}
Fix it: an import nobody uses
✓This program doesn't compile. Fix it so it shows Ready.
Ready
1package main2 3import (4 "fmt"5 "strings"6)7 8func main() {9 fmt.Println("Ready")10}
What you learned
- A Go program is
package mainwith afunc main(); it starts running atmain. import "fmt"brings infmt.Printlnandfmt.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.