Skip to content

Beginner · Getting started with Go · topic 2 of 46 · 30 min

Installing Go and the go command

Explanation

You install Go once from go.dev. After that, one command called go does almost everything: go run runs a program, go build makes a binary, go fmt formats code, go vet finds suspicious code, go test runs tests and go env shows settings.

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.

  1. Download the installer for your system from go.dev/dl.
  2. Run it (or unpack it on Linux) with the default settings.
  3. Close and reopen your terminal so it picks up the new PATH.
  4. Type go version. You should see something like go version go1.26.0 windows/amd64.
bash
go version
# go version go1.26.0 windows/amd64   (your numbers may differ)
If you see "go is not recognized" or "command not found", the PATH was not updated: reopen the terminal or log out and in.
Tip: Use the latest stable version unless your company says otherwise. Go keeps old code working, so upgrading is usually painless.

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

CommandWhat it does
go run .Compiles the program in this folder into a temporary file and runs it. Good while learning.
go buildCompiles 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 NAMEStarts a new module (next topic).
go get PATHAdds or updates a dependency.
go envShows Go settings such as GOPATH, GOOS and GOARCH.
go doc fmt.PrintlnShows 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.

go
package main
import "fmt"
func main(){
fmt.Println(  "hi" )
}
Before go fmt: messy but valid.
go
package main

import "fmt"

func main() {
	fmt.Println("hi")
}
After go fmt: the standard layout every Go programmer expects.

go vet: a second pair of eyes

go
package main

import "fmt"

func main() {
	name := "Asha"
	fmt.Printf("Hello %s, you are %d years old\n", name)
}
This compiles, but go vet reports that the format needs two values and only one was given.

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.
Careful: Old tutorials (before about 2019) say you must put all code inside GOPATH/src. That is out of date. With modules you can keep a project in any folder, such as D:\projects\hello.
Careful: Common mistake: running go run main.go in a folder with several .go files in package main. Only main.go is compiled, so functions from the other files are "undefined". Use go run . to compile the whole folder.

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

Install Go · meet the go command

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.

bash
go version
go env GOOS GOARCH
go build ./...
go vet ./...
go test ./...
A typical first-day sanity check in a Go project.

Practice (0/3 done)

Task 1 · Question

Which command compiles AND runs the program in the current folder, without keeping a binary?

Task 2 · Question

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?

Task 3 · Practice

Install Go on your computer and check the toolchain works.

Quick quiz

Question 1

1.What does go fmt do?

Question 2

2.What does ./... mean in go vet ./...?

Question 3

3.Which tool finds suspicious code that still compiles, like a wrong Printf format?

Question 4

4.With Go modules, where must your project folder be?

Read more (free)

Before this, learn

This topic builds on these. If something here feels new, open them first.

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.

From Environment variables and config

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.

From Installing Rust with rustup

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.

From Installing .NET and using the dotnet CLI

Where should database passwords for your project live?Related

Answer: In environment variables, never committed

Commit an example config without real secrets, if anything.

From Your portfolio project: a production-style Go API

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

  1. 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.
  2. Module 1 exam: Getting started with Go · 14 questions · 20 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, Control flow and functions. 46 topics left in the course before the final exam.

← Previous: What Go is and why people use it