Beginner

Hello World in Go

Your first Go program - the classic Hello World example with Docker setup

Every programming journey starts with Hello World. Let’s write our first Go program.

The Code

Create a file named hello.go:

1
2
3
4
5
6
7
package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
}

Understanding the Code

  • package main - Declares this file as part of the main package. Executable programs must have a main package.
  • import "fmt" - Imports the format package from Go’s standard library for formatted I/O.
  • func main() - The entry point of every Go executable. Execution starts here.
  • fmt.Println() - Prints a line to standard output followed by a newline.

Running with Docker

The easiest way to run this without installing Go locally:

1
2
3
4
5
# Pull the official Go image
docker pull golang:1.23

# Run the program
docker run --rm -v $(pwd):/app -w /app golang:1.23 go run hello.go

Running Locally

If you have Go installed:

1
2
3
4
5
6
# Run directly (compiles and executes in one step)
go run hello.go

# Or compile to binary first
go build hello.go
./hello

Expected Output

Hello, World!

Key Concepts

  1. Go is compiled - Source code is compiled directly to machine code (no VM)
  2. Package system - Every Go file belongs to a package
  3. main is special - The main function in the main package is the entry point
  4. Automatic formatting - Run go fmt hello.go to format your code (or use gofmt)
  5. Static linking - Go produces a single binary with no external dependencies

Why Go Is Different

Unlike Java or Python, Go compiles to a single static binary. This means:

  • No runtime dependencies needed
  • Easy deployment (just copy the binary)
  • Fast startup time

Next Steps

Continue to Variables and Data Types to learn about storing and manipulating data in Go.

Running Today

All examples can be run using Docker:

docker pull golang:1.23
Last updated: