Hello Go
Go (often called Golang) was created at Google in 2009 by Robert Griesemer, Rob Pike and Ken Thompson, one of the inventors of Unix and C. They wanted a language as fast as C++ that was as easy to read as Python, compiled in seconds, and made concurrency simple. Today Go runs a huge part of the cloud: Docker, Kubernetes, Terraform, Prometheus, Grafana, etcd, Caddy and the backends of Uber, Twitch, Cloudflare and Dropbox are written in it.
💡 About the examples in this track: Go can't run inside a web page here, so every example shows its Output underneath. Every program was run with Go 1.27 to produce that output exactly. Copy any example and run it with
go run main.go, or paste it into the official playground at go.dev/play.
Your first program
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}go run main.goHello, World!
| Part | Meaning |
|---|---|
package main |
Every Go file belongs to a package. main is special: it builds an executable program |
import "fmt" |
Use the standard library's formatting package |
func main() |
The program starts here |
fmt.Println(…) |
Print values followed by a newline. Names starting with a capital letter are exported (public) |
No semicolons (the compiler inserts them), and the opening { must be on the same line as func. Go code is indented with tabs.
Compiling to a single binary
go run compiles to a temporary file and runs it. go build produces a real executable:
go build -o hello main.go
./hello
ls -lh hello
file helloHello, World! 2.3M hello: ELF 64-bit LSB executable, ARM aarch64
The binary is statically linked: it contains everything it needs, including the Go runtime. Copy that one file to any Linux server and it runs, with no interpreter or libraries to install. That's a big reason Go is popular for command-line tools and containers (a Go app's Docker image can be a few megabytes).
Cross-compiling is just two environment variables:
GOOS=windows GOARCH=amd64 go build -o hello.exe main.go
GOOS=darwin GOARCH=arm64 go build -o hello-mac main.go
file hello.exe hello-machello.exe: PE32+ executable for MS Windows 10.00 (console) hello-mac: Mach-O 64-bit arm64 executable
Printing with fmt
package main
import "fmt"
func main() {
name := "Asha"
age := 31
height := 1.68
fmt.Println("Name:", name, "Age:", age)
fmt.Printf("%s is %d years old and %.2f m tall\n", name, age, height)
fmt.Printf("%v | %q | %T | %5d | %-6s|\n", age, name, height, 42, "go")
greeting := fmt.Sprintf("Hi %s!", name)
fmt.Println(greeting, len(greeting))
}Name: Asha Age: 31 Asha is 31 years old and 1.68 m tall 31 | "Asha" | float64 | 42 | go | Hi Asha! 8
| Verb | Prints |
|---|---|
%v |
Any value in its default format (the most used) |
%+v / %#v |
Structs with field names / as Go syntax |
%d, %f, %.2f |
Integers, floats |
%s, %q |
Strings, quoted strings |
%T |
The value's type |
%5d, %-6s |
Width: right-aligned, left-aligned |
Println adds spaces between values and a newline; Printf formats; Sprintf returns the formatted string instead of printing it.
The compiler is strict, on purpose
Go refuses to compile code with unused variables or imports. It keeps programs clean, especially after refactoring:
package main
import (
"fmt"
"os"
)
func main() {
count := 10
fmt.Println("hello")
}# command-line-arguments ./main.go:5:2: "os" imported and not used ./main.go:9:2: declared and not used: count
Errors are precise and short. Most editors fix imports for you automatically when you save (using goimports, part of the Go extension).
Formatting: gofmt
Go has one official code style, applied by a tool. Nobody argues about tabs or brace positions:
package main
import "fmt"
func main(){
x:=1;if x>0{fmt.Println( "positive" )}
}gofmt messy.gopackage main
import "fmt"
func main() {
x := 1
if x > 0 {
fmt.Println("positive")
}
}gofmt -w file.go (or go fmt ./... for a whole project) rewrites the files in place. Editors run it on save, so all Go code in the world looks the same, which makes reading other people's code much easier.
Installing Go
- Download the installer from go.dev/dl (Windows, macOS, Linux), or:
- macOS:
brew install go - Linux: unpack the official archive into
/usr/local/goand add/usr/local/go/binto yourPATH. (Distribution packages are often several versions behind.)
- macOS:
- Check:
go version. - Editor: VS Code with the official Go extension (it installs
gopls, the language server, for autocomplete and errors), or GoLand.
go versiongo version go1.27.4
Go at a glance
| Go | |
|---|---|
| Typing | Static, with type inference (x := 5) |
| Memory | Garbage collected: no malloc/free |
| Compiles to | One native binary per OS/CPU |
| Concurrency | Goroutines and channels, built into the language |
| Object model | Structs, methods and interfaces; no classes or inheritance |
| Errors | Returned as values, not exceptions |
| Standard library | Very complete: HTTP servers, JSON, crypto, testing, templates |
Go deliberately leaves features out (no inheritance, no exceptions, few ways to do each thing) so that code stays simple and readable by everyone on a team. You'll see that philosophy throughout this track.
Practice
- Print your name, age and city on one line with
Printf, using%s,%dand%q. - Print the type of
42,4.2,"42"andtruewith%T. - Build your program for Windows from Linux or macOS with
GOOS=windows. - Write some badly formatted code and fix it with
gofmt.
1.What does go build produce?
2.What happens if you declare a variable and never use it?
3.Which function returns a formatted string instead of printing it?
4.What makes a name exported (public) in Go?