Install Go, Check Your Path, and Initialize a Module
Verify your Go installation with go version and go env, initialize a module with go mod init, and fix go.mod not found errors. After this, you can start and build any Go project cleanly.
Skip ahead
Already have go version working on your PATH? Jump to Case 2: why go mod init is required or Case 3: why go run . beats go run main.go.
Installing Go takes two minutes, yet two small setup mistakes trip up almost every beginner on day one: go not appearing on the shell PATH (or `~/go/bin` missing when running tools installed with go install), and running loose .go files without a go.mod module file. Fixing both once keeps every future Go project clean and predictable. Verified on Go 1.27.
Case 1: Verify the Go binary and key go env paths
Open a fresh terminal after installing Go and verify both the compiler version and your binary/cache paths:
$ go version
go version go1.27.4 linux/amd64
$ go env GOROOT GOPATH GOMODCACHE
/usr/local/go
/root/go
/root/go/pkg/mod
GOROOT/bin(e.g./usr/local/go/bin): Holds thegocompiler itself. If your terminal sayscommand not found: go, add/usr/local/go/bin(Linux/macOS) orC:\Program Files\Go\bin(Windows) to yourPATHand open a new terminal.GOPATH/bin(usually~/go/bin): Wherego installdrops CLI tools likegopls,dlv, orstaticcheck. Add$HOME/go/binto yourPATHtoo so installed Go tools run directly by name.
Case 2: Initialize a module with go mod init
Modern Go requires a go.mod file at your project root so the compiler knows how to resolve local packages and pin external dependency versions. Create a directory, initialize the module, and run your first file:
$ mkdir hello && cd hello
$ go mod init example.com/hello
// main.go
package main
import "fmt"
func main() {
fmt.Println("Hello from Go!")
}
$ go run .
Hello from Go!
Use github.com/<your-user>/<repo> as the module path when you plan to push to GitHub, or example.com/<project> for local experiments.
Case 3: Why go run . works when go run main.go breaks
Watch what happens the moment you split a helper function into a sibling file greet.go inside the same package main folder:
// greet.go
package main
func formatGreeting(name string) string {
return "Welcome, " + name + "!"
}
// main.go
package main
import "fmt"
func main() {
fmt.Println(formatGreeting("Ana"))
}
$ go run main.go
# command-line-arguments
./main.go:6:14: undefined: formatGreeting
$ go run .
Welcome, Ana!
Passing a single filename (go run main.go) tells Go to compile only that file and ignore every sibling file in the folder. Passing . compiles the whole package together cleanly.
Case 4: Core commands to verify your workspace
$ gofmt -w . # format all .go files to standard style
$ go vet ./... # catch printf mismatches, copied locks, bad struct tags
$ go test ./... # run all unit tests across every package
$ go build -o hello . # compile a standalone native binary
💡 Tip
Do not set a custom GOROOT environment variable manually—the go binary locates its own installation directory automatically, and a stale GOROOT pointing to an older Go version breaks builds after upgrades.
Common mistakes
- Running
go run .in a folder with nogo.mod: Fails immediately withgo: go.mod file not found in current directory or any parent directory. Rungo mod init ...once at the repo root. - Putting an old
GOPATHworkflow inside~/go/src: Since Go 1.16+, modules work in any directory on disk (like~/projects/myapp). - Building a library package and wondering where the binary went:
go buildonly writes an executable when the directory ispackage mainwith afunc main().
⚠ Warning
On Linux, system package managers like apt install golang often ship Go versions that are 2–3 releases behind current Go (missing modern features like for b.Loop() or math/rand/v2). Install the official tarball from go.dev or use an official golang:trixie container.
What happens if you skip this
Missing PATH entries break editor tooling like gopls, and skipping go mod init or using go run main.go causes immediate undefined and module errors as soon as a project grows past one file.
Related course: Go for Beginners.
About
Verify your Go installation with go version and go env, initialize a module with go mod init, and fix go.mod not found errors. After this, you can start and build any Go project cleanly.
- Responsible
- Jay P
- Last Update
- 11/27/2026
- Reading Time
- 4 min
Reviews
Sign in to write a reviewComments
Sign in to commentNo comments yet.