Tutorials / Go: From Basics to Production / Installing Go 1.25 and your first program

Installing Go 1.25 and your first program

12 min read go
Course contents (17 lessons)
  1. Getting started with Go
  2. Installing Go 1.25 and your first program
  3. Variables, types and constants
  4. Control flow: if, for and switch
  5. Functions, multiple returns and defer
  6. Slices and maps
  7. Arrays and slices
  8. Maps and working with strings
  9. Structs, methods and interfaces
  10. Structs, methods and pointers
  11. Interfaces
  12. Errors, packages and generics
  13. Error handling the Go way
  14. Packages and modules
  15. Generics
  16. Concurrency
  17. Goroutines and channels
  18. select, timeouts and context
  19. Mutexes, sync and the race detector
  20. Testing
  21. Testing and benchmarks
  22. Building and shipping a service
  23. Building an HTTP JSON API with net/http
  24. 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/local and add /usr/local/go/bin to 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

CommandWhat it does
go run .Compiles and runs the package in this folder, without leaving a binary
go buildCompiles and writes an executable
go fmt ./...Formats all code
go vet ./...Finds suspicious code
go test ./...Runs tests
go mod tidyAdds 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.go in a multi-file package. It runs only that file. Use go run . to run the whole package.
  • Running Go commands outside a module. You get go.mod file not found. Run go mod init first 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.

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!")
	}
}

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