Code of the Day
BeginnerCore syntax

Hello, World!

Install Go, initialise a module with go mod init, and run your first program end-to-end.

GoBeginner8 min read
By the end of this lesson you will be able to:
  • 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. 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 . 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-world

The 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.22

That'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:

  1. package main — every .go file starts with a declaration. The special name main tells the compiler this is an executable, not a library.
  2. import "fmt" — the fmt package from the standard library handles formatted I/O. Go's standard library is wide and well-documented.
  3. func main() — the entry point. Every Go executable starts here, the same as main in C or main in Java.
  4. fmt.Println — not a macro, not a special form, just a regular function that prints a line. Package functions are always PackageName.FunctionName.

go run and go build

During development, use go run to compile and execute in one step:

go run main.go

Output:

Hello, World!

When you're ready to ship a binary, use go build:

go build -o hello-world .
./hello-world

go 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: fmt

The 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-go

Create 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.

Finished reading? Mark it complete to track your progress.

On this page