Beginner · Getting started with Go · topic 2 of 46 · 30 min
Installing Go and the go command
Explanation
Installing Go
Go has an official installer for Windows, macOS and Linux on the download page at go.dev/dl. On Windows you run the .msi file; on macOS the .pkg file; on Linux you unpack a .tar.gz file into /usr/local/go and add /usr/local/go/bin to your PATH. PATH is the list of folders your terminal searches when you type a command name.
- Download the installer for your system from go.dev/dl.
- Run it (or unpack it on Linux) with the default settings.
- Close and reopen your terminal so it picks up the new PATH.
- Type go version. You should see something like go version go1.26.0 windows/amd64.
go version
# go version go1.26.0 windows/amd64 (your numbers may differ)An editor
Any text editor works, but VS Code with the official Go extension (or GoLand, a paid IDE) gives you auto-complete, error underlines and automatic formatting on save. The VS Code extension uses gopls, the official Go language server: a background program that understands your code and tells the editor about it.
The go command: one tool for everything
| Command | What it does |
|---|---|
| go run . | Compiles the program in this folder into a temporary file and runs it. Good while learning. |
| go build | Compiles and saves a binary (hello, or hello.exe on Windows) that you can run again and again. |
| go fmt ./... | Rewrites your files into the one standard Go layout (tabs, spacing, alignment). |
| go vet ./... | Looks for code that compiles but is probably wrong, such as a Printf with the wrong number of values. |
| go test ./... | Runs your tests (module 5). |
| go mod init NAME | Starts a new module (next topic). |
| go get PATH | Adds or updates a dependency. |
| go env | Shows Go settings such as GOPATH, GOOS and GOARCH. |
| go doc fmt.Println | Shows the documentation of a function in the terminal. |
The ./... pattern means "this folder and every folder below it". So go vet ./... checks the whole project.
gofmt: no more arguments about style
Every Go file in the world is formatted the same way by gofmt (which go fmt runs). Tabs for indentation, the opening brace on the same line, and so on. Teams do not argue about style, and code from any company looks familiar. Most editors run it every time you save.
package main
import "fmt"
func main(){
fmt.Println( "hi" )
}package main
import "fmt"
func main() {
fmt.Println("hi")
}go vet: a second pair of eyes
package main
import "fmt"
func main() {
name := "Asha"
fmt.Printf("Hello %s, you are %d years old\n", name)
}go env and a few settings
- GOROOT: where Go itself is installed. You rarely touch it.
- GOPATH: a folder (by default go inside your home folder) where downloaded modules are cached and go install puts tools. You do NOT need to keep your projects inside it any more.
- GOOS and GOARCH: the operating system and processor you build for. You will use them for cross-compiling in module 9.
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
Installing Go and the go command · 1/7
Let us install Go and meet the one command that does almost everything: go.
Prefer a video by a teacher? Find free videos on “install go golang windows vs code setup go run go build” on YouTube ↗
In real life: A new joiner's first day
Ravi joins a Bengaluru fintech as a backend intern. His setup checklist is short: install Go from go.dev, install VS Code with the Go extension, clone the repository, then run go build ./... and go test ./... to see everything compile and pass. No virtual environments, no npm install with hundreds of packages. Before every commit, his editor runs gofmt and the CI pipeline runs go vet.
go version
go env GOOS GOARCH
go build ./...
go vet ./...
go test ./...Practice (0/3 done)
Which command compiles AND runs the program in the current folder, without keeping a binary?
Your folder has main.go and helpers.go, both in package main. go run main.go says "undefined: formatRupees", which is in helpers.go. What is the fix?
Install Go on your computer and check the toolchain works.
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Your first program and Go modulesGetting started with Go · same module
- Variables, basic types and zero valuesGetting started with Go · same module
- Environment variables and configBackend Development · Node.js and Express
- Installing Rust with rustupRust Programming · Getting started with Rust
- Installing .NET and using the dotnet CLIC# and .NET · Getting started with C# and .NET
- Your portfolio project: a production-style Go APICareers and interviews
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
What does go fmt do?
Answer: Formats code into the standard Go style
go fmt runs gofmt, which rewrites files into the one official layout.
What does ./... mean in go vet ./...?
Answer: This folder and all folders below it
The ... pattern matches every package under the current folder.
Which tool finds suspicious code that still compiles, like a wrong Printf format?
Answer: go vet
go vet reports likely mistakes such as format strings that do not match their arguments.
With Go modules, where must your project folder be?
Answer: Anywhere you like
Modules made the GOPATH layout optional. A project can live in any folder.
How can modern Node.js load a .env file without any package?Related
Answer: node --env-file=.env server.js
Node.js 20.6 and later understand the --env-file flag. Older projects use the dotenv package.
On Windows you see "linker link.exe not found". What is missing?Related
Answer: The Microsoft C++ build tools
Rust on Windows (the default MSVC toolchain) uses Microsoft's linker from the C++ build tools.
What do you need to install to BUILD C# programs?Related
Answer: The .NET SDK
The SDK has the compiler and the dotnet tools. The runtime alone can only run built apps.
Where should database passwords for your project live?Related
Answer: In environment variables, never committed
Commit an example config without real secrets, if anything.
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- 3 more topics in “Getting started with Go” before its exam: Your first program and Go modules, Variables, basic types and zero values, Constants, iota and type conversions.
- Module 1 exam: Getting started with Go · 14 questions · 20 min, without help. 5 topics of this module still to finish first.
- Then the next module, Control flow and functions. 46 topics left in the course before the final exam.