Go from Zero: Your First Program, go.mod, and Why Unused Imports Fail
What Go actually is, how to initialize a module with go mod init, and how to run and compile your first program—even if you have never written code.
You'll learn how Go turns your text files into a standalone executable program, how go mod init sets up a project, and why Go refuses to compile if you leave unused imports lying around. All you need is Go installed (go version in a terminal).
1. Go turns text into a self-contained machine
Some languages (like Python or JavaScript) need an interpreter—a separate program installed on the user's machine—to read and run your code line by line.
Go works differently. Go compiles your text files into a single, standalone binary program (like hello or hello.exe). You can hand that single file to someone who has never heard of Go, and it just runs.
Every Go project starts in a folder with a go.mod file that names your project and tracks its Go version and dependencies:
mkdir hello-go && cd hello-go
go mod init helloThat creates one tiny text file called go.mod. Think of it as the project's birth certificate.
2. Writing and running main.go
Create a file called main.go in that same folder:
package main
import "fmt"
func main() {
fmt.Println("Gerald has entered the terminal.")
}Run it from your terminal:
go run .Gerald has entered the terminal.Every line in that 7-line file has a specific job:
package main— tells Go: "This folder builds an executable program, not a reusable library."import "fmt"— brings in Go's standard formatting/printing toolkit (fmt, short for format).func main()— the front door. When your program starts, Go looks formaininsidepackage mainand starts executing there.fmt.Println(...)— prints a line of text to the terminal. NoticePrintlnstarts with a capitalP: in Go, capitalized names are public, and lowercase names are private to the package.
go run . compiles your code into a temporary folder, runs it, and deletes the temporary binary. When you want to keep the compiled program, run go build instead:
go build
./helloRun go build in hello-go, then run ./hello (or hello.exe on Windows). It prints the exact same line with zero Go tools needed at runtime.
3. Go is a strict neat-freak (on purpose)
Try adding an import you don't use, or removing "fmt" while calling fmt.Println:
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println("Hello")
}./main.go:5:2: "strings" imported and not usedGo refuses to compile. Not a polite yellow warning—a hard stop. Go's creators decided that unused imports and unused variables are almost always clutter or bugs, so the compiler forces you to clean them up immediately.
Also notice gofmt: run gofmt -w . in any Go folder and Go formats your spacing and indentation automatically, ending 100% of team arguments about tabs vs spaces.
Common mistakes
| Error | What it means | Fix |
|---|---|---|
go: go.mod file not found | Ran go run . in a folder without go.mod | Run go mod init myproject once in the project root |
imported and not used: "fmt" | Imported a package you didn't call | Remove the unused import line |
undefined: fmt | Forgot import "fmt" at the top | Add import "fmt" below package main |
syntax error: unexpected newline, expecting { | Put { on a new line after func main() | Keep { on the exact same line as func main() |
Practice
- What is the difference between
go run .andgo build? - Why does
fmt.println("hi")(lowercasep) fail to compile?
Answers:
go run .compiles and runs your program immediately without leaving a binary in your folder;go buildcompiles and saves the executable binary to disk.- In Go, only identifiers starting with a capital letter are exported (public) from a package. The function is
fmt.Println, notfmt.println.
What you learned
go mod init <name>initializes a Go project with ago.modfile.- Executable programs start at
func main()insidepackage main. - Go treats unused imports as compile errors and enforces consistent formatting with
gofmt.