Hello, World!
Install Go, initialise a module with go mod init, and run your first program end-to-end.
- Install Go and verify the installation with go version
- Initialise a module with go mod init and understand the file layout
- Write and run a program that prints to the terminal using fmt.Println
- Use go run for development and go build for a compiled binary
- Read a basic compiler error message without panicking
Every language has a ritual first program. Go's entry point looks almost the same as C's — but the toolchain you use to build and run it is one of Go's genuine strengths. Compile times are measured in milliseconds, the standard library covers an enormous range of tasks, and there's only one way to format code. Let's get it running.
Installing Go
Go is distributed as a single binary from go.dev/dl. Download the package for your platform, run the installer, and open a fresh terminal to verify:
go version # go version go1.22.3 linux/amd64 (something like that)On Linux/macOS you can also use your package manager (brew install go, apt install golang-go, etc.), though the official download usually has the freshest release.
go env shows your configuration. Run go env GOPATH and go env GOROOT to see where Go is installed and where your workspace lives. You rarely need to set these manually with modern Go — the module system handles it.
go mod init — the module system
Modern Go organises code with modules. A module is a directory tree of Go source files with a single go.mod file at the root. Create one:
mkdir hello-world
cd hello-world
go mod init example.com/hello-worldThe path after go mod init is the module path — a unique identifier, usually a URL you control. For personal projects, anything works. This creates go.mod:
module example.com/hello-world
go 1.22That's the whole file for now. When you add dependencies, Go writes them here automatically.
The program
Create a file main.go in the same directory:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}Four things to notice:
package main— every.gofile starts with a package declaration. The special namemaintells the compiler this is an executable, not a library.import "fmt"— thefmtpackage from the standard library handles formatted I/O. Go's standard library is wide and well-documented.func main()— the entry point. Every Go executable starts here, the same asmainin C ormainin Java.fmt.Println— not a macro, not a special form, just a regular function that prints a line. Package functions are alwaysPackageName.FunctionName.
go run and go build
During development, use go run to compile and execute in one step:
go run main.goOutput:
Hello, World!When you're ready to ship a binary, use go build:
go build -o hello-world .
./hello-worldgo build produces a self-contained native binary with no runtime dependencies. You can copy it to any machine with the same OS and architecture and it just runs.
go fmt and go vet. Go ships with two other commands worth knowing early. go fmt ./... reformats all your source files to the official style — there is only one Go style, and the tool enforces it. go vet ./... catches common mistakes the compiler doesn't reject (like a Printf format string that doesn't match its arguments). Run both before you consider code finished.
Reading a compiler error
Go's compiler is fast and gives precise errors. Try breaking the program — remove the import line and run it:
./main.go:4:2: undefined: fmtThe format is always file:line:column: message. The column number pinpoints exactly which token caused the issue. Once you're used to this format (covered in the Fundamentals track), Go errors are easy to act on.
Go will not compile unused imports. If you import a package and don't use anything from it, the compiler rejects the file. This is a deliberate design decision — it keeps dependency trees honest and compile times fast. The same rule applies to unused local variables.
Do it yourself
Type these commands — don't copy-paste:
mkdir my-first-go
cd my-first-go
go mod init example.com/my-first-goCreate main.go by hand, write the hello-world program, and run it with go run main.go. Then change the string to something personal. After that, run go build -o my-program . and execute the binary directly.
Where to go next
You have a working Go toolchain and understand the basic structure of a Go program. Next: variables and types — how Go declares variables, what zero values are, and the basic types that every Go program is built from.