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.
with homebrew:
sudo brew install go
with apt-get:
sudo apt-get install golang
install golang manually
or
compile it yourself
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:
- GolangSortingVisualization visualizes various sorting algorithms on the terminal or as gif
- golang-minigames currently only contains a snake clone
- bbmandelbrot.go calculates a mandelbrot fractal and saves it as png
- golibs contains various Go packages (e.g. math, converter, stack, cli, ...)
- cgol.go is conways game of life in Golang
- micromarkdownGo converts markdown to html (via regular expression)
- wikiGo is a wiki software in Go
- ...
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:
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
go run HelloWorld.go
Print Hello World with comments (shebang version)
./HelloWorldShebang.go
Declare variables and print them
go run var.go
Various ways (and styles) to print variables
go run printf.go
If statement in golang
go run if.go Hello
Declare array and print it's items
go run array.go
Declare your own functions
go run function.go
Do something multiple times
go run for.go
Read via cli provided input data
go run args.go string string2
Read via cli provided input data
go run input.go
Or scan for it
go run scan.go
Read named argument input data
go run flag.go
Return the working directory
go run dir.go
Return the current time/date in various formats
go run time.go
Return pseudo random integer values
go run random.go
Concat strings in two different ways
go run cat.go
Modulo operation finds the remainder of division
go run modulo.go
Split a string by another string and make an array from the result
go run split.go
An example implementation of the Ackermann function
go run ackermann.go
An example implementation of the Euclidean algorithm
go run euklid.go
Make pipeable unix applications with os.Stdin
go run pipe.go
Submit a function as argument
go run functioncallback.go
A function returned by a function
go run functionclosure.go
A function with an unknown amount of inputs (variadic function)
go run functionvariadic.go
Empty interface as argument (You Don't Know Type)
go run interfaces.go
Make structs (objects) which have functions
go run oop.go
AES-GCM encryption example
go run aesgcm.go
Bcrypt hashing example
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
go run in_array.go
Calculate triangles
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
go run wait.go
Last in - first out - example
go run lifo.go
Split a string via regular expression and make an array from the result
go run regex.go
More advanced regex (with time and dates)
go run regex2.go
Use my golibs regex package
go run regex3.go
Calculate and print the fibonacci numbers
go run fibonacci.go
Calculate and print the requested (32th) prime number
go run prime.go 32
Do things with numbers, strings and switch-cases
go run numbers.go
Pop and push in golang
go run lifo.go
List files in working directory
go run explorer.go
Start a ticker (do things periodically)
go run ticker.go
Do something in case of a timeout
go run timeout.go
Convert go object to json string
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
Run a self killing app
go run suicide.go
Calculate π 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 prime
Convert from rgb to hsl
go run color.go
Telnet with golang
go run telnet.go
The smallest golang http server
go run httpd.go
Secure golang http server
go run httpsd.go
The smallest golang http proxy
go run proxy.go
Read and write cookies
go run cookies.go
Demonstrate 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 false
A dynamic amount of channels
time go run dynparallel.go 8
Run the compiler and comment each line which contains an error
go build gocomment.go
./gocomment go-app.go
Convert a image to a grayscale and to a color inverted image
go run image.go
Sql (sqlite) golang example
go run sqlite.go insert test
go run sqlite.go select
Public-key/asymmetric cryptography signing and validating
go run ppk-crypto.go
Hashing (md5, sha) in go
go run hashing.go
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 © 2017 Simon Waldherr
Dual-licensed. See the LICENSE file for details.
if you like what i do feel free to support me
you can do so by:
- donate via PayPal or liberaPay
- buy me a beer at a conference
- give me a job where i can work on open source projects