Go, chapter by chapter · Lesson 1 of 62 · about 30 min
Getting started
Install Go, create a module, and understand every line of your first program and every command you will use daily.
On this page · 9 sections
Install Go#
- Download the installer for your system from go.dev/dl and run it.
- Open a new terminal and check it worked:
go version
# go version go1.25.x linux/amd64- Install an editor with Go support. VS Code with the official Go extension is the most common choice. It installs
gopls, the Go language server that gives you autocomplete, go-to-definition and inline errors.
Create a module#
A module is a project: a folder with a go.mod file at its root. The module path is the name other code uses to import it, conventionally the place it will live on GitHub.
mkdir godsa && cd godsa
go mod init github.com/you/godsaThat creates go.mod:
module github.com/you/godsa
go 1.25Your first program#
Create main.go in the same folder:
package main
import "fmt"
// main is where the program starts.
func main() {
fmt.Println("Hello, gopher")
}Run it:
go run .
# Hello, gopherReading the program line by line#
| Line | What it means |
|---|---|
package main | Every .go file starts by naming its package. main is special: it produces an executable instead of a library. |
import "fmt" | Brings in the standard library's formatting package. You can only use packages you import. |
// main is where… | A comment. // runs to the end of the line; /* … */ can span lines. |
func main() { | Declares a function named main with no inputs and no outputs. The program begins here and ends when it returns. |
fmt.Println(…) | Calls the Println function from package fmt. Capitalized names like Println are exported: usable from other packages. |
"Hello, gopher" | A string literal. Strings use double quotes. |
Two rules surprise newcomers. First, the opening brace { must be on the same line as func main(); Go inserts semicolons automatically at line ends, and a brace on the next line would end the statement too early. Second, every import and every local variable must be used, or the program does not compile.
Change it and run it again
The fastest way to learn is to edit a working program and predict what will change. This one adds a variable, a loop and fmt.Printf, which fills in the % placeholders from its other arguments:
package main
import "fmt"
func main() {
name := "gopher" // := declares a variable and infers its type
fmt.Println("Hello,", name)
for i := 1; i <= 3; i++ {
fmt.Printf("lap %d of %d: %s is running\n", i, 3, name)
}
fmt.Printf("%q has %d letters\n", name, len(name)) // %q adds quotes
fmt.Printf("%v %v %v\n", 42, 3.5, true) // %v prints any value
}Hello, gopher
lap 1 of 3: gopher is running
lap 2 of 3: gopher is running
lap 3 of 3: gopher is running
"gopher" has 6 letters
42 3.5 trueNow break it on purpose to see the two rules from above. Add an unused variable inside main:
unused := 5and go run . refuses to compile (the line number depends on where you put it):
./main.go:7:2: declared and not used: unusedThen move the { after func main() onto its own line and run again. Reading compiler errors calmly is a skill you will use every day.
The commands you will use every day#
| Command | What it does |
|---|---|
go run . | Compile the package in this folder and run it immediately. |
go build | Compile to an executable file you can ship. |
go test ./... | Run every test in this folder and all subfolders. ./... means "here and below". |
go vet ./... | Report suspicious code that compiles but is probably wrong. |
gofmt -w . | Rewrite files in the standard format. Configure your editor to do this on save. |
go doc fmt.Println | Show documentation for anything in the terminal. |
go mod tidy | Add missing dependencies to go.mod and remove unused ones. |
How a program is organized#
godsa/
├── go.mod module github.com/you/godsa
├── main.go package main (the executable)
└── stack/
├── stack.go package stack (a library)
└── stack_test.go tests for package stack- One folder is one package. All files in a folder share the same
packageline. - Other code imports a folder by module path plus folder:
import "github.com/you/godsa/stack". - Test files end in
_test.goand are only compiled bygo test.
You will build exactly this layout over the course, adding one package per data structure.
Summary#
- Install Go, then use
go mod initto start every project. package mainplusfunc main()makes a program; any other package name makes a library.- Unused imports and variables are errors, braces stay on the same line, and
gofmtdecides formatting. go run .,go test ./...,go vet ./...andgo doccover most of your daily work.
Check your understanding
Answer before scrolling back up. If you miss one, reread the matching section, then try again.
Q1.Which package name turns a Go program into an executable?
Q2.You write
import "os"but never use it. What happens?Q3.What does
go mod init github.com/you/godsacreate?
Practice in Go
Write a table test first, then the solution. On LeetCode, choose Go as the language. Open a hint only after 15 minutes stuck.
- Set up your godsa modulebuild
Create a folder, run
go mod init, write a program that prints your name and today's date using thetimepackage, and run it withgo run ..Show hint
time.Now().Format("2006-01-02")prints the date. Go's layout uses that exact reference date. - Break it on purposebuild
Remove a closing brace, misspell
Println, and add an unused variable, one at a time. Read each compiler message carefully.Show hint
Compiler errors show
file:line:column. Learning to read them fast saves hours later. - Push to GitHubbuild
Put the module in a public GitHub repo called
godsa. Every later lesson adds a folder to it.
Finished this lesson?
Mark it done to track your progress. It is saved in this browser only.