Hello, World
The smallest runnable Go program - package main, the main function, and fmt.Println.
The smallest complete Go program proves the toolchain is wired up and your code runs. Go requires exactly one main package and one main function as the entry point - no global scripts, no implicit execution.
Every Go program lives in a package. The main package is special: the compiler turns it into an executable binary. fmt.Println writes its arguments to stdout followed by a newline.
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}Run it directly with go run:
$ go run hello.go
Hello, World!Or compile it into a binary first:
$ go build -o hello hello.go
$ ./hello
Hello, World!Go was designed for the cloud era - go build produces a single statically linked binary with no runtime dependency, which is why Go services fit naturally into scratch or distroless container images.
In production
fmt.Println writes plain text to stdout. In containerised services, replace it with a structured logger - zerolog or zap - that writes JSON. Plain text logs are unqueryable in Datadog or Grafana Loki, bypass log levels, and make request-ID serialisation impossible. In containers, write to stderr (not stdout) unless your orchestrator explicitly reads stdout; mixing application logs with stdout output from sub-processes is a common source of log corruption in production.
Enjoyed this? Get more essays on software craft delivered to your inbox.
Subscribe free