Small, focused programs for learning Go—from Hello, World! to concurrency,
WebAssembly, runtime tracing, and modern language features. Most examples are
standalone programs and can be run independently.
Use the online editor to edit and run examples in the browser, or play the WebAssembly-based Qix game.
The module version in go.mod is the source of truth for the
required Go toolchain.
git clone https://github.com/SimonWaldherr/golang-examples.git
cd golang-examples
go run ./beginner/HelloWorld.go
go run ./advanced/iterators
./scripts/check-modern.sh| Directory | What you will find |
|---|---|
beginner/ |
Syntax, types, control flow, files, and small algorithms |
advanced/ |
Generics, iterators, concurrency, encoding, tests, and benchmarks |
expert/ |
Servers, tracing, assembly, CGO, cryptography, and image generation |
non-std-lib/ |
Examples that depend on third-party packages or external services |
tinygo/ |
TinyGo and microcontroller examples |
These examples intentionally use recent stable Go features:
| Go version | Feature | Run it |
|---|---|---|
| 1.23 | Range-over-function iterators plus iter, maps, and slices |
go run ./advanced/iterators |
| 1.24 | Generic type aliases | go run ./advanced/generic-alias |
| 1.24 | Benchmarks with testing.B.Loop |
go test ./advanced/benchmark-loop -bench . |
| 1.25 | sync.WaitGroup.Go |
go run ./advanced/waitgroup-go |
| 1.25 | Deterministic concurrent tests with testing/synctest |
go test ./advanced/synctest |
| 1.25 | Runtime trace flight recorder | go run ./expert/flight-recorder |
| 1.26 | Initialized pointers with new(expression) |
go run ./beginner/initialized-pointer |
| 1.21 | Structured JSON logging with log/slog |
go run ./advanced/structured-logging |
The flight-recorder example writes flight.trace; inspect it with
go tool trace flight.trace.
nanoGo is a minimalist interpreter for a supported subset of Go. It evaluates source code dynamically in a CLI, REPL, embedded host, or browser playground; when used in the browser, the interpreter itself is compiled to WebAssembly. That makes nanoGo a strong fit for interactive tutorials, editable documentation, controlled snippets, and browser-based Go experiments. Try the nanoGo playground or embed it in a web page.
| nanoGo | TinyGo | GopherJS | |
|---|---|---|---|
| Execution model | Interprets supported Go source at runtime | Compiles Go programs ahead of time | Compiles Go programs ahead of time to JavaScript |
| Browser artifact | The interpreter runs in WASM and evaluates guest source dynamically | The application itself can be compiled to WASM | Pure JavaScript generated from the application |
| Best suited to | Playgrounds, REPLs, live examples, controlled embedded scripting, and teaching | Microcontrollers, embedded applications, and deployable WASM programs | Browser front ends and JavaScript-based web integrations |
| Go compatibility | Deliberately supported language and library subset | A compiler with its own documented Go compatibility differences | Broad Go support with documented browser and JavaScript-runtime constraints |
| Host control | Optional capabilities and cooperative resource limits can restrict guest source | The compiled program runs for its selected target; TinyGo is not an interpreter sandbox | The generated program executes with normal browser JavaScript capabilities |
nanoGo is therefore not a smaller replacement for TinyGo or GopherJS. Choose nanoGo when source must be edited or evaluated at runtime; choose TinyGo when you want to compile and deploy an application, for example to a Raspberry Pi Pico; choose GopherJS when compiled JavaScript is the right browser target.
The GitHub Pages editor
uses its go2js integration, which is
based on GopherJS. Pressing F5 formats the editor contents,
compiles the Go program to JavaScript, and evaluates that JavaScript in the
page. It is a compile-and-run workflow, not a nanoGo interpreter session.
The following curated list contains public, non-fork repositories from SimonWaldherr that complement this collection.
- golang-benchmarks — examples for measuring Go code
- GolangSortingVisualization — visualized sorting algorithms
- golibs — general-purpose Go packages
- gotools — a collection of small Go tools
- GoRealtimeWeb — real-time web application examples
- mdExec — executes code blocks in Markdown files
- nanoGo — a Go-subset interpreter for native hosts and WebAssembly, with a playground, REPL, CLI, and embeddable host API
- tinySQL — an educational SQL engine written in pure Go
- tinyRAG — a lightweight retrieval-augmented generation system
- smallR — a small R-like environment written in Go
- DataDock — a server-side database web interface
- golang-minigames — small games written in Go
- bbmandelbrotGo — Mandelbrot image generation
- FluidSimASCII — an ASCII fluid simulator
- vango — image-manipulation effects
- rp2040-examples and rpi-examples — Raspberry Pi and RP2040 examples
- RGB-LED-Matrix and pico75player — LED-matrix projects
All are published as free and open-source software. Browse the complete Go repository search for more.
- macOS with Homebrew:
brew install go - Debian/Ubuntu:
sudo apt install golang-go - Other systems: follow the official Go installation guide
The examples are divided into beginner, advanced, expert, third-party, and TinyGo sections. Commands in the catalog below are shown relative to the corresponding directory unless they include a directory prefix.
To execute a Golang program, write go run at the cli followed by the name of the file.
You also can convert the file to a binary executable program by the command go build.
If you know #!, also known as Shebang, there is an equivalent for go: //usr/bin/env go run $0 $@ ; exit
Print Hello World with comments (Golang Playground)
go run HelloWorld.goPrint Hello World with comments (shebang version)
./HelloWorldShebang.goDeclare variables and print them (Golang Playground)
go run var.goVarious ways (and styles) to print variables (Golang Playground)
go run printf.goIf statement in Golang (Golang Playground)
go run if.go HelloDeclare array and print its items (Golang Playground)
go run array.goDeclare your own functions (Golang Playground)
go run function.goDo something multiple times (Golang Playground)
go run for.goRead via cli provided input data (Golang Playground)
go run args.go string string2Read via cli provided input data (Golang Playground)
go run input.goOr scan for it (Golang Playground)
go run scan.goRead named argument input data (Golang Playground)
go run flag.goReturn the working directory (Golang Playground)
go run dir.goReturn the current time/date in various formats (Golang Playground)
go run time.goReturn pseudo random integer values (Golang Playground)
go run random.goConcat strings in two different ways (Golang Playground)
go run cat.goModulo operation finds the remainder of division (Golang Playground)
go run modulo.goSplit a string by another string and make an array from the result (Golang Playground)
go run split.goAn example implementation of the Ackermann function (Golang Playground)
go run ackermann.goAn example implementation of the Euclidean algorithm (Golang Playground)
go run euklid.goSubmit a function as argument (Golang Playground)
go run functioncallback.goA function returned by a function (Golang Playground)
go run functionclosure.goA function with an unknown amount of inputs (variadic function) (Golang Playground)
go run functionvariadic.goEmpty interface as argument (You Don't Know Type) (Golang Playground)
go run interface.goExecute Shell/Bash commands and print its output values (Golang Playground)
go run shell.goMake structs (objects) which have functions (Golang Playground)
go run oop.goDependency injection for easier testing
cd beginner/di
go testHashing (md5, sha) in go (Golang Playground)
go run hashing.goError handling – creating, returning, wrapping and inspecting errors
go run error.goSwitch statement – expression switch, condition switch, type switch and fallthrough
go run switch.goType conversions – numeric casts, string/[]byte/[]rune and strconv helpers
go run typeconv.goBenchmarking example (using JSON marshal and unmarshal for the sample) (Golang Playground)
From the root directory ($GOPATH/github.com/SimonWaldherr/golang-examples), run this command:
go test -bench=. -benchmem advanced/json_bench/main_test.goMake pipe-able unix applications with os.Stdin (Golang Playground)
go run pipe.goAES-GCM encryption example (Golang Playground)
go run aesgcm.goBcrypt hashing example (Golang Playground)
Please install package golang.org/x/crypto/bcrypt before run this file by running go get golang.org/x/crypto/bcrypt
go run bcrypt.goSearch element is exist in arrays or not (Golang Playground)
go run in_array.goCalculate triangles (Golang Playground)
go run pythagoras.go (float|?) (float|?) (float|?)Read from stdin (but don't wait for the enter key)
go run getchar.goWait and sleep (Golang Playground)
go run wait.goLast in - first out - example (Pop and push in Golang) (Golang Playground)
go run lifo.goSplit a string via regular expression and make an array from the result (Golang Playground)
go run regex.goMore advanced regex (with time and dates) (Golang Playground)
go run regex2.goUse my golibs regex package and have fun (Golang Playground)
go run regex3.goCalculate and print the fibonacci numbers (Golang Playground)
go run fibonacci.goCalculate and print the requested (32th) prime number (Golang Playground)
go run prime.go 32Do things with numbers, strings and switch-cases (Golang Playground)
go run numbers.goUse a template to create and fill documents (this example uses LaTeX) (Golang Playground)
go run template.go
pdflatex -interaction=nonstopmode template_latex.texStart a ticker (do things periodically)
go run ticker.goDo something in case of a timeout (Golang Playground)
go run timeout.goConvert go object to json string (Golang Playground)
go run json.goRun unix/shell commands in go apps
go run exec.goCompress by pipe
go run compress.goCompress by file
go run compress2.goParse CSV (Golang Playground)
go run csv.goConvert CSV to a Markdown table (Golang Playground)
go run csv2md.goParse a XML string into a Struct with undefined Fields (Golang Playground)
go run xml.goRun a self killing app
go run suicide.goGoCV : hello video
go run hello_video.goGoCV : face detection
go run face_detect.go 0 model/haarcascade_frontalface_default.xmlRun the example for generic (Golang Playground)
go run generic.goProtect shared state with sync.Mutex and sync.RWMutex
go run mutex.goContext cancellation, timeouts, deadlines, and value propagation
go run context.goWorker pool – distribute jobs across a fixed number of goroutines
go run workerpool.goCalculate π with go (leibniz, euler and prime are running until you stop it via CTRL+C)
go run pi2go.go leibniz
go run pi2go.go euler
go run pi2go.go primeCalculate π with go - same as above - but with live output (based on gcurses)
go run pi2go-live.go leibniz
go run pi2go-live.go euler
go run pi2go-live.go primeList files in working directory
go run explorer.gorun assembly code from golang
go run assembly.gorun C code from golang
go run cgo.gogenerate Go code with golang templates
go run codegen.goConvert from rgb to hsl (Golang Playground)
go run color.goTelnet with Golang
go run telnet.goThe smallest Golang http server
go run httpd.goSecure Golang http server
go run httpsd.goThe smallest Golang http proxy
go run proxy.goRead and write cookies
go run cookies.goDemonstrate the power of multithreading / parallel computing you have to set GOMAXPROCS to something greater than 1 to see any effect
export GOMAXPROCS=8
time go run parallel.go true
time go run parallel.go falseA dynamic amount of channels
time go run dynparallel.go 8Run the compiler and comment each line which contains an error
go build gocomment.go
./gocomment go-app.goConvert a image to a grayscale and to a color inverted image
go run image.goGenerate an image with three colored circles (with intersection)
go run image2.goGenerate an image representing the Mandelbrot fractal
go run image3.goSql (sqlite) Golang example
maybe you also wanna take a look at my sql-examples-project
go run sqlite.go insert test
go run sqlite.go selectPublic-key/asymmetric cryptography signing and validating
go run ppk-crypto.goCommand Line Arguments Golang Example We can get argument values though command line by specifying the operator '-' with the name of the argument and the value to be set. E.g. -env=qa
go run command_line_arguments.go
go run command_line_arguments.go -env=qa -consumer=trueCron Golang Example We can trigger a function at a particular time through cron
go run cron.goMap Golang Example Hash Map standard functions in golang
go run map.goToken-bucket rate limiter – throttle request throughput with burst support
go run ratelimiter.goYou can even use Go on microcontrollers, the keyword here is TinyGo, a go compiler specially developed for SBCs and MCUs.
This is distinct from nanoGo: TinyGo
compiles programs for deployment, while nanoGo interprets a supported Go subset
dynamically for interactive and embedded-host use cases.
If you want to blink the LED of your Raspberry Pi Pico, try this:
tinygo build -o firmware.uf2 -target=pico ./tinygo/blink.goand then upload it to the pico.
One great aspect of Golang is, that you can start go applications via go run name.go, but also compile it to an executable with go build name.go. After that you can start the compiled version which starts much faster.
If you start fibonacci.go and the compiled version you will notice, that the last line which contains the execution time doesn't differ much, but if you start it with time ./fibonacci 32 and time go run ./fibonacci.go 32 you will see the difference.
Copyright © 2026 Simon Waldherr Dual-licensed. See the LICENSE file for details.