Go Playbook

Course › Week 1 · Basics and a first CLI › Day 1 · lesson 1 of 42 · 10 min reading + 45–60 min practice

The go command and your first program

Create a module, run and build it, and read go.mod the way you read composer.json.

go mod initgo.modgo rungo buildpackage mainfmt.Println

On this page
  1. Create a module
  2. The program
  3. Your first compiler errors
  4. The other go commands you need
  5. Coming from PHP
  6. Practice
  7. Remember

Today is about the tool you will type most: the go command. It creates projects, downloads packages, compiles, runs, tests and formats. In PHP terms it is php, Composer, PHPUnit and Pint in one program.

By the end you will have created a module, read every file in it, run it, built it into a program you can copy to a server, and seen two compiler errors. Then the first pages of A Tour of Go.

Create a module

1. go mod init

A Go project is a module: a folder with a go.mod file in it. Make the folder, then let go mod init write the file.

terminal
mkdir hello
cd hello
go mod init hello
Output
go: creating new go.mod: module hello

hello is the module’s name. For a module you publish, the name is where it lives, like github.com/you/logstat, so that go get can find it. Week 2 covers that. A local name is fine until then.

2. go.mod

The project file. It is short:

go.mod
module hello

go 1.27.4

module is your package’s name, like name in composer.json. The go line is the Go version the module needs, like "php": "^8.4" in require. When you add a package, a require block appears below it, and a second file, go.sum, records the checksum of every download, like composer.lock.

The program

3. main.go

Create main.go next to go.mod. Every Go program starts in a function called main, in a package called main.

main.go
package main

import "fmt"

func main() {
	fmt.Println("Hello, world!")
}
$ go run .
Hello, world!

Three parts, in this order, in every Go file. package main says which package the file belongs to; main is the one that becomes a program. import "fmt" brings in the standard library’s formatting package, like a use line. func main() is where the program starts.

fmt.Println prints its arguments and a newline. You call a package’s function with the package name in front: fmt.Println, never just Println. There are no semicolons: Go adds them at the end of each line for you.

go run . means “compile the package in this folder and run it”. The dot is the folder.

4. go build makes a program

go run compiles to a temporary file and runs it. go build compiles and keeps the result: a single executable file named after the module.

terminal
go build
ls
./hello
Output
go.mod
hello
main.go
Hello, world!

PHP reads your source every time it runs. Go compiles it once into one file, here hello, about 2.3 MB. That file has everything inside it: copy it to a server with the same operating system and run it, with no Go installed there. That is why Go works so well for tools you hand to a Laravel app later in this course.

It can also build for another system from yours. GOOS=linux GOARCH=amd64 go build on a Mac makes a program for a Linux server.

5. Printing values

fmt.Println takes any number of values and puts a space between them. fmt.Printf takes a template with verbs: %s for a string, %d for a whole number, %v for any value. Printf does not add a newline: end the template with \n.

main.go
package main

import "fmt"

func main() {
	app := "Nordic Stays"
	bookings := 1250

	fmt.Println(app, "has", bookings, "bookings")
	fmt.Printf("%s has %d bookings\n", app, bookings)
}
$ go run .
Nordic Stays has 1250 bookings
Nordic Stays has 1250 bookings

:= creates a variable. There is no $. Variables get their own lesson next; today, just print them.

Your first compiler errors

6. A typo in a name

In PHP a misspelled variable is a warning at runtime, and the code carries on with null. Go refuses to build:

main.godoes not compile
package main

import "fmt"

func main() {
	bookings := 1250

	fmt.Println(bookings, "bookings")
	fmt.Println(booking, "bookings")
}
go build
# hello
./main.go:9:14: undefined: booking

Read it from the left. # hello is the package being built. ./main.go:9:14 is the file, the line and the column. Then one sentence: booking does not exist.

7. An import you do not use

This one surprises every PHP developer. A program that imports a package and never uses it does not build:

main.godoes not compile
package main

import (
	"fmt"
	"os"
)

func main() {
	fmt.Println("Hello, world!")
}
go build
# hello
./main.go:5:2: "os" imported and not used

Several imports go in parentheses, one per line. Go treats an unused import, and an unused variable, as an error, not a warning. It keeps programs free of leftovers, and it costs you nothing: the editor removes unused imports and adds missing ones each time you save.

The other go commands you need

CommandWhat it doesWhen
go run .Compiles the package in this folder and runs it.To try your program
go buildCompiles and keeps the executable.To ship it, or to run it many times
go fmt ./...Formats every file in the one standard Go style. ./... means this folder and every folder below it.Your editor does it on save
go vet ./...Finds code that compiles but is almost certainly wrong.Before every commit
staticcheck ./...The linter from Day 0: more checks than go vet.Before every commit
go get <module>Adds a package to go.mod.When you need a library
go doc fmt.PrintlnPrints the documentation of anything, in the terminal.Whenever you wonder what a function takes

The standard library is large: HTTP servers and clients, JSON, CSV, SQL, logging and tests are all in it. Most of this course uses nothing else. Other packages live in Git repositories and are listed at pkg.go.dev, Go’s Packagist.

Coming from PHP

Most of what you know from Composer has a direct Go equivalent.

PHPGo
composer.jsongo.mod
composer.lockgo.sum (checksums; the versions themselves are in go.mod)
vendor/downloaded once to ~/go/pkg/mod, shared by every project
composer require guzzlehttp/guzzlego get github.com/jackc/pgx/v5 (an example: HTTP needs no package)
Packagistpkg.go.dev (an index; the code stays in its Git repository)
php script.phpgo run .
Pintgofmt, built in, with no options
PHPStan / Larastango vet, staticcheck, and the compiler itself

The biggest difference is the compiler. In PHP, a typo or a wrong type shows up when that line runs, maybe in production. In Go the program does not exist until every name and every type checks out.

PracticeA Tour of Go: welcome and the first basics pages, one small program

In A Tour of Go, do Hello, 世界, then the basics pages Packages, Imports and Exported names. Change each program and run it again; the Tour runs your edit in the browser.

Exercise: a booking summary

Create a module called summary whose program prints three lines: a property name, the number of nights, and the guest count, each from its own variable, with Printf. Then break it on purpose: misspell a variable, remove a quote, remove a brace, import os without using it. Read each error before you fix it.

Show the solution
main.gonew file
package main

import "fmt"

func main() {
	property := "Nyhavn flat"
	nights := 3
	guests := 2

	fmt.Printf("Property: %s\n", property)
	fmt.Printf("Nights:   %d\n", nights)
	fmt.Printf("Guests:   %d\n", guests)
}
$ go run .
Property: Nyhavn flat
Nights:   3
Guests:   2

Remember

  • go run . to try, go build to ship; one module per folder, created with go mod init.
  • An unused import or variable is a compile error: delete it, do not silence it.

Tomorrow: every variable has exactly one type, and a variable you never set is not null but zero.