golang-examples
Go(lang) examples - (explain the basics of #golang)
Science Score: 64.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
✓CITATION.cff file
Found CITATION.cff file -
✓codemeta.json file
Found codemeta.json file -
✓.zenodo.json file
Found .zenodo.json file -
○DOI references
-
✓Academic publication links
Links to: zenodo.org -
✓Committers with academic emails
2 of 57 committers (3.5%) from academic institutions -
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (8.4%) to scientific vocabulary
Keywords
Keywords from Contributors
Repository
Go(lang) examples - (explain the basics of #golang)
Basic Info
- Host: GitHub
- Owner: SimonWaldherr
- License: mit
- Language: Go
- Default Branch: master
- Homepage: https://simonwaldherr.github.io/golang-examples/
- Size: 2.63 MB
Statistics
- Stars: 1,636
- Watchers: 55
- Forks: 458
- Open Issues: 6
- Releases: 5
Topics
Metadata Files
readme.md
Go Examples
Use the online live editor with Golang support. Edit and run the examples directly in your browser:
If you liked this project, you may also like
my golang-benchmarks repository:
my gotools repository:
my sql-examples repository:
my rp2040-examples repository:
or my rpi-examples repository:
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:
Shell
sudo brew install go
with apt-get:
Shell
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:
- 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 a Mandelbrot 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 is Conway's Game of Life in Golang
- 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:
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)
Shell
go run HelloWorld.go
Print Hello World with comments (shebang version)
Shell
./HelloWorldShebang.go
Declare variables and print them (Golang Playground)
Shell
go run var.go
Various ways (and styles) to print variables (Golang Playground)
Shell
go run printf.go
If statement in Golang (Golang Playground)
Shell
go run if.go Hello
Declare array and print its items (Golang Playground)
Shell
go run array.go
Declare your own functions (Golang Playground)
Shell
go run function.go
Do something multiple times (Golang Playground)
Shell
go run for.go
Read via cli provided input data (Golang Playground)
Shell
go run args.go string string2
Read via cli provided input data (Golang Playground)
Shell
go run input.go
Or scan for it (Golang Playground)
Shell
go run scan.go
Read named argument input data (Golang Playground)
Shell
go run flag.go
Return the working directory (Golang Playground)
Shell
go run dir.go
Return the current time/date in various formats (Golang Playground)
Shell
go run time.go
Return pseudo random integer values (Golang Playground)
Shell
go run random.go
Concat strings in two different ways (Golang Playground)
Shell
go run cat.go
Modulo operation finds the remainder of division (Golang Playground)
Shell
go run modulo.go
Split a string by another string and make an array from the result (Golang Playground)
Shell
go run split.go
An example implementation of the Ackermann function (Golang Playground)
Shell
go run ackermann.go
An example implementation of the Euclidean algorithm (Golang Playground)
Shell
go run euklid.go
Submit a function as argument (Golang Playground)
Shell
go run functioncallback.go
A function returned by a function (Golang Playground)
Shell
go run functionclosure.go
A function with an unknown amount of inputs (variadic function) (Golang Playground)
Shell
go run functionvariadic.go
Empty interface as argument (You Don't Know Type) (Golang Playground)
Shell
go run interface.go
Execute Shell/Bash commands and print its output values (Golang Playground)
Shell
go run shell.go
Make structs (objects) which have functions (Golang Playground)
Shell
go run oop.go
Dependency injection for easier testing
Shell
cd beginner/di
go test
Hashing (md5, sha) in go (Golang Playground)
Shell
go run hashing.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:
Shell
go test -bench=. -benchmem advanced/json_bench/main_test.go
Make pipe-able unix applications with os.Stdin (Golang Playground)
Shell
go run pipe.go
AES-GCM encryption example (Golang Playground)
Shell
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
Shell
go run bcrypt.go
Search element is exist in arrays or not (Golang Playground)
Shell
go run in_array.go
Calculate triangles (Golang Playground)
Shell
go run pythagoras.go (float|?) (float|?) (float|?)
Read from stdin (but don't wait for the enter key)
Shell
go run getchar.go
Wait and sleep (Golang Playground)
Shell
go run wait.go
Last in - first out - example (Pop and push in Golang) (Golang Playground)
Shell
go run lifo.go
Split a string via regular expression and make an array from the result (Golang Playground)
Shell
go run regex.go
More advanced regex (with time and dates) (Golang Playground)
Shell
go run regex2.go
Use my golibs regex package and have fun (Golang Playground)
Shell
go run regex3.go
Calculate and print the fibonacci numbers (Golang Playground)
Shell
go run fibonacci.go
Calculate and print the requested (32th) prime number (Golang Playground)
Shell
go run prime.go 32
Do things with numbers, strings and switch-cases (Golang Playground)
Shell
go run numbers.go
Use a template to create and fill documents (this example uses LaTeX) (Golang Playground)
Shell
go run template.go
pdflatex -interaction=nonstopmode template_latex.tex
Start a ticker (do things periodically)
Shell
go run ticker.go
Do something in case of a timeout (Golang Playground)
Shell
go run timeout.go
Convert go object to json string (Golang Playground)
Shell
go run json.go
Run unix/shell commands in go apps
Shell
go run exec.go
Compress by pipe
Shell
go run compress.go
Compress by file
Shell
go run compress2.go
Parse CSV (Golang Playground)
Shell
go run csv.go
Convert CSV to a Markdown table (Golang Playground)
Shell
go run csv2md.go
Parse a XML string into a Struct with undefined Fields (Golang Playground)
Shell
go run xml.go
Run a self killing app
Shell
go run suicide.go
GoCV : hello video
Shell
go run hello_video.go
GoCV : face detection
Shell
go run face_detect.go 0 model/haarcascade_frontalface_default.xml
Run the example for generic (Golang Playground)
Shell
go run generic.go
Expert
Calculate π with go (leibniz, euler and prime are running until you stop it via CTRL+C)
Shell
go run pi2go.go leibniz
go run pi2go.go euler
go run pi2go.go prime
Calculate π with go - same as above - but with live output (based on gcurses)
Shell
go run pi2go-live.go leibniz
go run pi2go-live.go euler
go run pi2go-live.go prime
List files in working directory
Shell
go run explorer.go
run assembly code from golang
Shell
go run assembly.go
run C code from golang
Shell
go run cgo.go
generate Go code with golang templates
Shell
go run codegen.go
Convert from rgb to hsl (Golang Playground)
Shell
go run color.go
Telnet with Golang
Shell
go run telnet.go
The smallest Golang http server
Shell
go run httpd.go
Secure Golang http server
Shell
go run httpsd.go
The smallest Golang http proxy
Shell
go run proxy.go
Read and write cookies
Shell
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
Shell
export GOMAXPROCS=8
time go run parallel.go true
time go run parallel.go false
A dynamic amount of channels
go
time go run dynparallel.go 8
Run the compiler and comment each line which contains an error
Shell
go build gocomment.go
./gocomment go-app.go
Convert a image to a grayscale and to a color inverted image
Shell
go run image.go
Generate an image with three colored circles (with intersection)
Shell
go run image2.go
Generate an image representing the Mandelbrot fractal
Shell
go run image3.go
Sql (sqlite) Golang example
maybe you also wanna take a look at my sql-examples-project
Shell
go run sqlite.go insert test
go run sqlite.go select
Public-key/asymmetric cryptography signing and validating
Shell
go run ppk-crypto.go
Command 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
Shell
go run command_line_arguments.go
go run command_line_arguments.go -env=qa -consumer=true
Cron Golang Example We can trigger a function at a particular time through cron
Shell
go run cron.go
Map Golang Example Hash Map standard functions in golang
Shell
go run map.go
TinyGo
You can even use Go on microcontrollers, the keyword here is TinyGo, a go compiler specially developed for SBCs and MCUs.
If you want to blink the LED of your Raspberry Pi Pico, try this:
Shell
tinygo build -o firmware.uf2 -target=pico ./tinygo/blink.go
and then upload it to the pico.
Compile
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.
License
Copyright © 2024 Simon Waldherr Dual-licensed. See the LICENSE file for details.
Owner
- Name: Simon Waldherr
- Login: SimonWaldherr
- Kind: user
- Location: Bavaria, Germany
- Company: RUBIX GmbH
- Website: https://simonwaldherr.de/
- Twitter: SimonWaldherr
- Repositories: 294
- Profile: https://github.com/SimonWaldherr
curr. #DataAnalyst, orig. #MechatronicsEngineer, hobby #Programmer #ComputerScience, deputy #FireBrigade commander, hobby #Photographer, loving #OpenSource
Citation (CITATION.cff)
cff-version: 1.2.0 message: "If you refer to this project, please cite it as below." authors: - family-names: "Waldherr" given-names: "Simon" title: "golang-examples" version: 1.14.0 doi: 10.5281/zenodo.5362197 date-released: 2021-09-01 url: "https://github.com/SimonWaldherr/golang-examples"
GitHub Events
Total
- Watch event: 74
- Delete event: 3
- Issue comment event: 2
- Push event: 1
- Pull request event: 8
- Fork event: 9
- Create event: 2
Last Year
- Watch event: 74
- Delete event: 3
- Issue comment event: 2
- Push event: 1
- Pull request event: 8
- Fork event: 9
- Create event: 2
Committers
Last synced: 7 months ago
Top Committers
| Name | Commits | |
|---|---|---|
| Simon Waldherr | g****t@s****u | 156 |
| mkoppmann | me@m****t | 5 |
| Aashna-Agrawal | A****l | 4 |
| arpit529srivastava | a****9@g****m | 4 |
| Yusuf Syaifudin | y****n@g****m | 3 |
| Swaraj Pande | s****4@g****m | 3 |
| FuXiang Shu | 6****f | 3 |
| Ranjan Raghavendra | 6****a | 3 |
| simonwaldherrZITEC | s****r@z****e | 2 |
| Jeremy Atkinson | j****n@g****m | 2 |
| Aashna Agrawal | A****l@g****m | 2 |
| Hannes Wagner | h****s@m****o | 2 |
| Joseph | j****3@o****m | 2 |
| Dane Elwell | d****l@u****k | 1 |
| CodeLingo Bot | b****t@c****o | 1 |
| Chanwit Piromplad | k****3@g****m | 1 |
| CJ Sampson | c****1@g****m | 1 |
| Ayush Karn | 7****7 | 1 |
| Anthony Alves | a****s@f****u | 1 |
| AngusReid | a****0@g****m | 1 |
| Aman | a****1@n****n | 1 |
| Adithya Pai B | p****6@g****m | 1 |
| vanaraj-az | v****z@g****m | 1 |
| midhatdrops | n****e@g****m | 1 |
| matbur95 | m****z@b****l | 1 |
| irfan nurhakim | m****a@g****m | 1 |
| Simon Waldherr | g****b@s****u | 1 |
| Eric Berry | c****b@g****m | 1 |
| Marcus Willock | c****m@g****m | 1 |
| i-Gagan | i****m@g****m | 1 |
| and 27 more... | ||
Committer Domains (Top 20 + Academic)
Issues and Pull Requests
Last synced: 4 months ago
All Time
- Total issues: 12
- Total pull requests: 84
- Average time to close issues: 11 months
- Average time to close pull requests: about 1 month
- Total issue authors: 9
- Total pull request authors: 64
- Average comments per issue: 2.17
- Average comments per pull request: 0.89
- Merged pull requests: 69
- Bot issues: 0
- Bot pull requests: 3
Past Year
- Issues: 0
- Pull requests: 6
- Average time to close issues: N/A
- Average time to close pull requests: 9 days
- Issue authors: 0
- Pull request authors: 3
- Average comments per issue: 0
- Average comments per pull request: 0.67
- Merged pull requests: 2
- Bot issues: 0
- Bot pull requests: 3
Top Authors
Issue Authors
- SimonWaldherr (3)
- crazcalm (2)
- priyankamantala (1)
- chyroc (1)
- ghost (1)
- jbuberel (1)
- vallabhiaf (1)
- Aarav238 (1)
- midhatdrops (1)
Pull Request Authors
- Aashna-Agrawal (6)
- dependabot[bot] (6)
- mkoppmann (4)
- Arpit529Srivastava (3)
- swarajpande4 (3)
- yusufsyaifudin (3)
- SimonWaldherr (2)
- sw-rubix (2)
- SleepySlash (2)
- crazcalm (2)
- haannnees (2)
- jchatkinson (2)
- midhatdrops (1)
- yeouchien (1)
- petersondmg (1)
Top Labels
Issue Labels
Pull Request Labels
Packages
- Total packages: 2
- Total downloads: unknown
-
Total dependent packages: 0
(may contain duplicates) -
Total dependent repositories: 0
(may contain duplicates) - Total versions: 66
proxy.golang.org: github.com/SimonWaldherr/golang-examples
- Homepage: https://github.com/SimonWaldherr/golang-examples
- Documentation: https://pkg.go.dev/github.com/SimonWaldherr/golang-examples#section-documentation
- License: MIT
-
Latest release: v1.24.2
published over 1 year ago
Rankings
proxy.golang.org: github.com/simonwaldherr/golang-examples
- Homepage: https://github.com/simonwaldherr/golang-examples
- Documentation: https://pkg.go.dev/github.com/simonwaldherr/golang-examples#section-documentation
- License: MIT
-
Latest release: v1.24.2
published over 1 year ago