Installing Go 1.25 and your first program
Course contents (17 lessons)
- Getting started with Go
- Installing Go 1.25 and your first program
- Variables, types and constants
- Control flow: if, for and switch
- Functions, multiple returns and defer
- Slices and maps
- Arrays and slices
- Maps and working with strings
- Structs, methods and interfaces
- Structs, methods and pointers
- Interfaces
- Errors, packages and generics
- Error handling the Go way
- Packages and modules
- Generics
- Concurrency
- Goroutines and channels
- select, timeouts and context
- Mutexes, sync and the race detector
- Testing
- Testing and benchmarks
- Building and shipping a service
- Building an HTTP JSON API with net/http
- Building and deploying a Go service
Why Go
Go was created at Google and released in 2009. Its goals were simple: fast builds, easy-to-read code, and good support for programs that do many things at once. It compiles to a single native binary with no runtime to install, which makes deployment pleasantly boring. I reach for it when I want a small service or a command-line tool that just runs.
Installing
Download the installer for your system from go.dev/dl. This course uses Go 1.25. Later versions will work too, because Go keeps old programs compatible.
- Windows: run the MSI installer. It adds Go to your PATH.
- macOS: run the package installer, or use
brew install go. - Linux: extract the archive to
/usr/localand add/usr/local/go/binto your PATH.
Open a new terminal and check:
go version
You should see something like go version go1.25.0 windows/amd64. If the command is not found, open a fresh terminal so the new PATH is picked up.
Your first module
Go code lives in modules. A module is a folder with a go.mod file that names it and records the Go version and dependencies. Create a folder and start one:
mkdir hello
cd hello
go mod init example.com/hello
The name example.com/hello is the module path. For code you publish, it is usually the repository address, such as github.com/you/hello. For learning, any name works. The command creates a go.mod with a line like go 1.25.
Hello, shop
Create main.go:
package main
import (
"fmt"
"os"
)
func main() {
shop := "Murtaza Stationers"
fmt.Println("Welcome to", shop)
if len(os.Args) > 1 {
fmt.Printf("Hello, %s!\n", os.Args[1])
} else {
fmt.Println("Hello, stranger!")
}
}
Run it:
go run .
go run . Ali
The output is:
Welcome to Murtaza Stationers
Hello, stranger!
Welcome to Murtaza Stationers
Hello, Ali!
What each line means
package main says this file belongs to the main package. A package named main is special: it builds into an executable program. Every Go file starts with a package line.
The import block lists the packages we use. fmt prints and formats text. os gives access to the operating system, including the command line arguments in os.Args. Here is a rule you will notice quickly: Go refuses to compile if you import something and do not use it. That feels strict at first, but it keeps code tidy.
func main() is where the program starts. Go uses tabs for indentation, and the standard formatter gofmt decides all layout, so there are no style arguments in Go teams. Your editor will usually run it on save. You can also run go fmt ./... yourself.
fmt.Println prints its arguments with spaces between them and a newline. fmt.Printf takes a format string with verbs such as %s for a string, %d for an integer and %v for "any value in a default format". Notice that Printf does not add a newline, so we write \n.
The go command
| Command | What it does |
|---|---|
go run . | Compiles and runs the package in this folder, without leaving a binary |
go build | Compiles and writes an executable |
go fmt ./... | Formats all code |
go vet ./... | Finds suspicious code |
go test ./... | Runs tests |
go mod tidy | Adds missing and removes unused dependencies |
Try go build and look at the folder. You now have hello.exe (or hello on macOS and Linux). Copy it to another computer of the same type and it runs without Go installed.
Common mistakes
- Unused imports and variables. The error looks like
"os" imported and not used. Remove the import. The editor can do it for you. - Running
go run main.goin a multi-file package. It runs only that file. Usego run .to run the whole package. - Running Go commands outside a module. You get
go.mod file not found. Rungo mod initfirst or move to the right folder. - Opening brace on a new line. In Go,
func main()and its{must be on the same line. Otherwise you get a syntax error. - Using double quotes versus single. Double quotes make strings. Single quotes make a single character (a rune), which is a different type.
Try it yourself
Change the program so that it prints your own shop or project name, and greets every name given on the command line, one per line. Hint: use for _, name := range os.Args[1:]. We will explain for properly soon. Run go vet and go build on your result.
package main
import (
"fmt"
"os"
)
func main() {
shop := "Murtaza Stationers"
fmt.Println("Welcome to", shop)
if len(os.Args) > 1 {
fmt.Printf("Hello, %s!\n", os.Args[1])
} else {
fmt.Println("Hello, stranger!")
}
}
This course is free to read. Create a free account to enrol, save your progress and pick up where you left off.
Sign up free I already have an account