Go Guide

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
  1. 01Install Go
  2. 02Create a module
  3. 03Your first program
  4. 04Reading the program line by line
  5. 05The commands you will use every day
  6. 06How a program is organized
  7. 07Summary
  8. 08Check your understanding
  9. 09Practice in Go

Install Go#

  1. Download the installer for your system from go.dev/dl and run it.
  2. Open a new terminal and check it worked:
Shell
go version
# go version go1.25.x linux/amd64
  1. 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.

Shell
mkdir godsa && cd godsa
go mod init github.com/you/godsa

That creates go.mod:

module github.com/you/godsa
 
go 1.25

Your first program#

Create main.go in the same folder:

main.go
Go
package main
 
import "fmt"
 
// main is where the program starts.
func main() {
	fmt.Println("Hello, gopher")
}

Run it:

Shell
go run .
# Hello, gopher

Reading the program line by line#

LineWhat it means
package mainEvery .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:

main.go
Go
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
}
Output
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 true

Now break it on purpose to see the two rules from above. Add an unused variable inside main:

Go
	unused := 5

and go run . refuses to compile (the line number depends on where you put it):

./main.go:7:2: declared and not used: unused

Then 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#

CommandWhat it does
go run .Compile the package in this folder and run it immediately.
go buildCompile 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.PrintlnShow documentation for anything in the terminal.
go mod tidyAdd 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 package line.
  • Other code imports a folder by module path plus folder: import "github.com/you/godsa/stack".
  • Test files end in _test.go and are only compiled by go test.

You will build exactly this layout over the course, adding one package per data structure.

Summary#

  • Install Go, then use go mod init to start every project.
  • package main plus func main() makes a program; any other package name makes a library.
  • Unused imports and variables are errors, braces stay on the same line, and gofmt decides formatting.
  • go run ., go test ./..., go vet ./... and go doc cover most of your daily work.

Check your understanding

Answer before scrolling back up. If you miss one, reread the matching section, then try again.

  1. Q1.Which package name turns a Go program into an executable?

  2. Q2.You write import "os" but never use it. What happens?

  3. Q3.What does go mod init github.com/you/godsa create?

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 the time package, and run it with go 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.