MCPcopy Index your code
hub / github.com/SimonWaldherr/golang-examples

github.com/SimonWaldherr/golang-examples @v1.25.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v1.25.0 ↗ · + Follow
629 symbols 1,219 edges 200 files 146 documented · 23%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Go Examples

DOI Go Report Card License: MIT

Use the online live editor with Golang support. Edit and run the examples directly in your browser: SimonWaldherr/golang-benchmarks Online Editor

Play an online game based on Qix implemented in Golang, compiled to WebAssembly and running in the browser: Qix Game

If you liked this project, you may also like
my golang-benchmarks repository:
SimonWaldherr/golang-benchmarks - GitHub
my gotools repository:
SimonWaldherr/gotools - GitHub
my sql-examples repository:
SimonWaldherr/sql-examples - GitHub
my rp2040-examples repository:
SimonWaldherr/rp2040-examples - GitHub
my rpi-examples repository:
SimonWaldherr/rpi-examples - GitHub
my nanoGo repository:
SimonWaldherr/nanoGo - GitHub
my tinySQL repository:
SimonWaldherr/tinySQL - GitHub
or my tinyRAG repository:
SimonWaldherr/tinyRAG - GitHub

About

These examples explain the basics of Golang. There will be more examples from time to time.

if you like, feel free to add more Golang examples. Many thanks to all contributors.

Install go(lang)

with homebrew:

sudo brew install go

with apt-get:

sudo apt-get install golang

install Golang manually or compile it yourself

Examples

The examples are divided into three levels of difficulty. The Beginner section contains very easy examples, starting with Hello World but also containing a few easy algorithms. The Advanced section uses more complicated features of Golang. Finally, the Expert section contains applications like telnet-clients or http-server (even with SSL). If you want even more Golang examples, you can take a look at my other go repositories at GitHub:

All of them are published as free and open source software.

If all of this is even not enough for you, you can take a look at the following websites:

Beginner

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

Print Hello World with comments (shebang version)

./HelloWorldShebang.go

Declare variables and print them (Golang Playground)

go run var.go

Various ways (and styles) to print variables (Golang Playground)

go run printf.go

If statement in Golang (Golang Playground)

go run if.go Hello

Declare array and print its items (Golang Playground)

go run array.go

Declare your own functions (Golang Playground)

go run function.go

Do something multiple times (Golang Playground)

go run for.go

Read via cli provided input data (Golang Playground)

go run args.go string string2

Read via cli provided input data (Golang Playground)

go run input.go

Or scan for it (Golang Playground)

go run scan.go

Read named argument input data (Golang Playground)

go run flag.go

Return the working directory (Golang Playground)

go run dir.go

Return the current time/date in various formats (Golang Playground)

go run time.go

Return pseudo random integer values (Golang Playground)

go run random.go

Concat strings in two different ways (Golang Playground)

go run cat.go

Modulo operation finds the remainder of division (Golang Playground)

go run modulo.go

Split a string by another string and make an array from the result (Golang Playground)

go run split.go

An example implementation of the Ackermann function (Golang Playground)

go run ackermann.go

An example implementation of the Euclidean algorithm (Golang Playground)

go run euklid.go

Submit a function as argument (Golang Playground)

go run functioncallback.go

A function returned by a function (Golang Playground)

go run functionclosure.go

A function with an unknown amount of inputs (variadic function) (Golang Playground)

go run functionvariadic.go

Empty interface as argument (You Don't Know Type) (Golang Playground)

go run interface.go

Execute Shell/Bash commands and print its output values (Golang Playground)

go run shell.go

Make structs (objects) which have functions (Golang Playground)

go run oop.go

Dependency injection for easier testing

cd beginner/di
go test

Hashing (md5, sha) in go (Golang Playground)

go run hashing.go

Error handling – creating, returning, wrapping and inspecting errors

go run error.go

Switch statement – expression switch, condition switch, type switch and fallthrough

go run switch.go

Type conversions – numeric casts, string/[]byte/[]rune and strconv helpers

go run typeconv.go

Advanced

Benchmarking 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.go

Make pipe-able unix applications with os.Stdin (Golang Playground)

go run pipe.go

AES-GCM encryption example (Golang Playground)

go run aesgcm.go

Bcrypt 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.go

Search element is exist in arrays or not (Golang Playground)

go run in_array.go

Calculate triangles (Golang Playground)

go run pythagoras.go (float|?) (float|?) (float|?)

Read from stdin (but don't wait for the enter key)

go run getchar.go

Wait and sleep (Golang Playground)

go run wait.go

Last in - first out - example (Pop and push in Golang) (Golang Playground)

go run lifo.go

Split a string via regular expression and make an array from the result (Golang Playground)

go run regex.go

More advanced regex (with time and dates) (Golang Playground)

go run regex2.go

Use my golibs regex package and have fun (Golang Playground)

go run regex3.go

Calculate and print the fibonacci numbers (Golang Playground)

go run fibonacci.go

Calculate and print the requested (32th) prime number (Golang Playground)

go run prime.go 32

Do things with numbers, strings and switch-cases (Golang Playground)

go run numbers.go

Use a template to create and fill documents (this example uses LaTeX) (Golang Playground)

go run template.go
pdflatex -interaction=nonstopmode template_latex.tex

Start a ticker (do things periodically)

go run ticker.go

Do something in case of a timeout (Golang Playground)

go run timeout.go

Convert go object to json string (Golang Playground)

go run json.go

Run unix/shell commands in go apps

go run exec.go

Compress by pipe

go run compress.go

Compress by file

go run compress2.go

Parse CSV (Golang Playground)

go run csv.go

Convert CSV to a Markdown table (Golang Playground)

go run csv2md.go

Parse a XML string into a Struct with undefined Fields (Golang Playground)

go run xml.go

Run a self killing app

go run suicide.go

GoCV : hello video

go run hello_video.go

GoCV : face detection

```Shell go run face_detect.go 0 model/haarcascade_frontalface_defau

Extension points exported contracts — how you extend this code

ApiClient (Interface)
(no doc) [3 implementers]
non-std-lib/gomock/client.go
Int (Interface)
Int is a type constraint. It can be used to indicate that a function can accept the types listed within the constraint,
advanced/generic/generic.go
ImageSet (Interface)
(no doc) [1 implementers]
expert/image.go
Codec (Interface)
(no doc) [1 implementers]
advanced/json2.go
Number (Interface)
(no doc)
expert/generics.go
Float (Interface)
Float is also a type constraint but describe type float32 and float64.
advanced/generic/generic.go
Number (Interface)
The Number used the Int and Float constraint. In this case it has the same effect as type Number interface { int | i
advanced/generic/generic.go
Number (Interface)
(no doc)
advanced/generic/generic2.go

Core symbols most depended-on inside this repo

Set
called by 17
expert/image.go
Add
called by 16
non-std-lib/ebiten-3-body-problem.go
printx
called by 15
beginner/printf.go
Read
called by 12
non-std-lib/packages/lorem.go
f1
called by 12
beginner/if2.go
f2
called by 10
beginner/if2.go
Run
called by 9
non-std-lib/ebiten-qix.go
Done
called by 9
non-std-lib/websocket_client.go

Shape

Function 474
Method 89
Struct 51
Interface 9
TypeAlias 6

Languages

Go100%

Modules by API surface

expert/webMiddleware.go26 symbols
advanced/oop.go22 symbols
non-std-lib/ebiten-3-body-problem.go20 symbols
non-std-lib/ebiten-qix.go16 symbols
advanced/banker.go16 symbols
non-std-lib/ebiten-snake.go12 symbols
advanced/generic/generic.go10 symbols
non-std-lib/pi2go-live.go9 symbols
non-std-lib/websocket_client.go8 symbols
non-std-lib/pi2go.go8 symbols
non-std-lib/defaultoptional.go8 symbols
non-std-lib/MongoDB/database.go8 symbols

Datastores touched

(mongodb)Database · 1 repos

For agents

$ claude mcp add golang-examples \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page