Your First Go Program
In this lesson you'll write the classic first program, run it with go run, turn it into a standalone executable with go build, and learn what each line means. You'll also meet two of the Go compiler's strictest rules, which catch real bugs before your program ever runs.
Hello, World
Make a new folder, create a file called hello.go in it, and type:
package main import "fmt" func main() { fmt.Println("Hello, World!") }
In a terminal, inside that folder, run it:
go run hello.go
Hello, World!
go run compiles the program into a temporary executable, runs it, and throws the executable away. It's what you'll use most while learning.
Line by line
| Code | Meaning |
|---|---|
package main | Every Go file starts by naming its package. Package main is special: it builds an executable program rather than a library. |
import "fmt" | Brings in the standard library's fmt package ("format"), which prints and formats text. |
func main() { … } | Declares the function where the program starts. It takes no arguments and returns nothing. When main returns, the program ends. |
fmt.Println(…) | Calls the Println function from package fmt. It prints its arguments separated by spaces, followed by a newline. |
Notice what's missing: no semicolons at the ends of lines, no class wrapped around main, and no parentheses needed for the import. Names from another package are always written package.Name, and they start with a capital letter — in Go, a capital first letter is what makes a name visible outside its package (lesson 43).
Printing more
package main import "fmt" func main() { fmt.Println("Go", "is", "fun") fmt.Println("2 + 3 =", 2+3) fmt.Print("Print adds ", "no newline. ") fmt.Print("See?\n") fmt.Printf("%s was released in %d\n", "Go 1.0", 2012) }
Go is fun 2 + 3 = 5 Print adds no newline. See? Go 1.0 was released in 2012
Println adds spaces and a newline for you, Print doesn't, and Printf fills placeholders such as %s (a string) and %d (a whole number) from its other arguments. Lesson 12 covers all the formatting options.
Building an executable
go build compiles the program and keeps the result:
go build hello.go
./hello # on Windows: .\hello.exe
Hello, World!
The executable contains everything it needs, including the Go runtime, so you can copy it to another computer with the same operating system and run it there — no Go installation required. To build for a different system, set GOOS and GOARCH; for example GOOS=linux GOARCH=amd64 go build hello.go produces a Linux program, even on a Mac.
Two rules the compiler enforces
Go refuses to compile code with an unused import or an unused local variable. Leftovers like these are usually a sign of a mistake, so Go treats them as errors rather than warnings:
package main import ( "fmt" "os" ) func main() { message := "Hello" fmt.Println("Hi") }
./hello.go:5:2: "os" imported and not used ./hello.go:9:2: declared and not used: message
Each error gives the file, line and column. Delete the unused lines (or use them) and the program compiles. Editors with the Go extension add and remove imports automatically when you save.
The opening brace
Go inserts semicolons at the ends of lines for you, behind the scenes. A side effect is that an opening brace { must go on the same line as the func, if or for it belongs to:
package main import "fmt" func main() { fmt.Println("Hello") }
./hello.go:6:1: syntax error: unexpected semicolon or newline before {There's only one accepted brace style, and gofmt enforces it, so all Go code looks the same.
Try it yourself
Change the program to print your name and your age next year.
Hello, my name is Sam Next year I'll be 30.
go command.