Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork458
Go(lang) examples - (explain the basics of #golang)
License
SimonWaldherr/golang-examples
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Use theonline live editor with Golang support. Edit and run the examples directly in your browser:
If you liked this project, you may also like
mygolang-benchmarks repository:
mygotools repository:
mysql-examples repository:
myrp2040-examples repository:
or myrpi-examples repository:
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 allcontributors.
withhomebrew:
sudo brew install go
withapt-get:
sudo apt-get install golang
install Golang manuallyorcompile it yourself
The examples are divided into three levels of difficulty. TheBeginner section contains very easy examples, starting withHello World but also containing a few easy algorithms. TheAdvanced section uses more complicated features of Golang. Finally, theExpert 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:
- golang-benchmarks shows how to benchmark the execution time of Golang functions
- GolangSortingVisualization visualizes various sorting algorithms on the terminal or as gif
- golang-minigames currently only contains a snake clone
- bbmandelbrot.go calculates aMandelbrot Fractal and saves it as PNG
- golibs contains various Go packages (e.g. math, converter, stack, cli, ...)
- fsagent watch a folder for new or modified files and do something
- cgol.go isConway'sGame of Life inGolang
- micromarkdownGo converts markdown to html (via regular expression)
- wikiGo is a wiki software in Go
- WorkingTimeMeasurementSystem demonstrates how to create a simple time tracking system using Golang and SQLite
- zplgfa is an image converter to print pictures on zpl compatible labels
- ...
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 aGolang program, writego run
at the cli followed by the name of the file.
You also can convert the file to a binary executable program by the commandgo build
.
If you know#!
, also known asShebang, 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 theworking 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/digotest
Hashing (md5, sha) in go (Golang Playground)
go run hashing.go
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:
gotest -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 runninggo 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 mygolibs 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 usesLaTeX) (Golang Playground)
go run template.gopdflatex -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
go run face_detect.go 0 model/haarcascade_frontalface_default.xml
Run the example for generic (Golang Playground)
go run generic.go
Calculate π with go (leibniz, euler and prime are running until you stop it via CTRL+C)
go run pi2go.go leibnizgo run pi2go.go eulergo run pi2go.go prime
Calculate π with go - same as above - but with live output (based ongcurses)
go run pi2go-live.go leibnizgo run pi2go-live.go eulergo run pi2go-live.go prime
List files in working directory
go run explorer.go
run assembly code from golang
go run assembly.go
run C code from golang
go run cgo.go
generate Go code with golang templates
go run codegen.go
Convert from rgb to hsl (Golang Playground)
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 computingyou have to set GOMAXPROCS to something greater than 1 to see any effect
export GOMAXPROCS=8time go run parallel.gotruetime go run parallel.gofalse
A dynamic amount of channels
timegorun dynparallel.go8
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
Generate an image with three colored circles (with intersection)
go run image2.go
Generate an image representing the Mandelbrot fractal
go run image3.go
Sql (sqlite) Golang example
maybe you also wanna take a look at mysql-examples-project
go run sqlite.go inserttestgo run sqlite.goselect
Public-key/asymmetric cryptography signing and validating
go run ppk-crypto.go
Command Line Arguments Golang ExampleWe 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.gogo run command_line_arguments.go -env=qa -consumer=true
Cron Golang ExampleWe can trigger a function at a particular time through cron
go run cron.go
Map Golang ExampleHash Map standard functions in golang
go run map.go
You can even use Go on microcontrollers, the keyword here isTinyGo, a go compiler specially developed for SBCs and MCUs.
If you want to blink the LED of your Raspberry Pi Pico, try this:
tinygo build -o firmware.uf2 -target=pico ./tinygo/blink.go
and then upload it to the pico.
One great aspect of Golang is, that you can start go applications viago run name.go
, but also compile it to an executable withgo 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 withtime ./fibonacci 32
andtime go run ./fibonacci.go 32
you will see the difference.
Copyright © 2024 Simon WaldherrDual-licensed. See theLICENSE file for details.
About
Go(lang) examples - (explain the basics of #golang)
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.