Go Get Started
You can run every example in this course right here in the browser. Sooner or later, though, you will want Go on your own computer. This chapter shows how to install it, create a project and run it.
Install Go
- Download the installer for your system from the official site, go.dev/dl.
- Run it (Windows
.msi, macOS.pkg) or, on Linux, unpack the archive into/usr/localand add/usr/local/go/binto yourPATH. - Open a new terminal and check the version:
go versiongo version go1.25.0 linux/amd64
Your version number and platform may differ — anything from Go 1.22 upward works for this course.
Note: You also want an editor with Go support. VS Code with the official Go extension is a popular free choice; it formats your code on save and shows errors as you type.
Create a module
Go code is organised in modules. A module is a folder with a go.mod file that records the module's name and the Go version it needs. Create a folder and initialise a module inside it:
mkdir hello
cd hello
go mod init example.com/hellogo: creating new go.mod: module example.com/hello
The module path (example.com/hello) is just a unique name. For code you publish it is usually the repository URL, e.g. github.com/yourname/hello. The generated go.mod looks like this:
module example.com/hello
go 1.25.0Write the program
Create a file called main.go in the same folder:
package main
import "fmt"
func main() {
fmt.Println("Hello from my first Go module!")
}Hello from my first Go module!
Run it
go run compiles the program into a temporary location and runs it immediately — perfect while you are developing:
go run .Hello from my first Go module!
The . means "the package in the current folder". You can also name a file: go run main.go.
Build an executable
go build compiles your program into a standalone executable file that you can copy to another machine with the same operating system — no Go installation needed there.
go build
ls
./hellogo.mod hello main.go
Hello from my first Go module!
On Windows the file is called hello.exe.
The most useful go commands
| Command | What it does |
|---|---|
go run . |
compile and run the package in the current folder |
go build |
compile to an executable |
go mod init <path> |
start a new module |
go mod tidy |
add missing and remove unused dependencies in go.mod |
go fmt ./... |
format all Go files in the module |
go vet ./... |
report suspicious code |
go test ./... |
run the tests |
go doc fmt.Println |
show documentation for a symbol |
A second program
Programs can import more than one package. Here strings is used to convert text to upper case.
package main
import (
"fmt"
"strings"
)
func main() {
name := "gopher"
fmt.Println("Hello,", strings.ToUpper(name))
}Hello, GOPHER
Notice the grouped import in parentheses — that is how Go code imports several packages. gofmt sorts them alphabetically for you.
Tip: Run
go fmt(or let your editor do it) before every commit. Nobody in the Go community argues about tabs vs. spaces — the tool decides, and it uses tabs.
Test yourself
Which command creates a new go.mod file?
go newgo mod initgo creatego build
What is the difference between go run and go build?
- There is none
go runcompiles and runs immediately;go buildwrites an executable filego buildruns testsgo runonly works on Linux