{"id":15127487,"url":"https://github.com/simonwaldherr/golang-examples","last_synced_at":"2025-05-14T06:07:30.988Z","repository":{"id":8048815,"uuid":"9459712","full_name":"SimonWaldherr/golang-examples","owner":"SimonWaldherr","description":"Go(lang) examples - (explain the basics of #golang)","archived":false,"fork":false,"pushed_at":"2025-03-13T00:48:45.000Z","size":2752,"stargazers_count":1605,"open_issues_count":7,"forks_count":457,"subscribers_count":55,"default_branch":"master","last_synced_at":"2025-04-03T14:42:03.571Z","etag":null,"topics":["education","examples","go","golang","hacktoberfest","hacktoberfest-accepted","howto","learning","programming-language"],"latest_commit_sha":null,"homepage":"https://simonwaldherr.github.io/golang-examples/","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SimonWaldherr.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":"CITATION.cff","codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"simonwaldherr","patreon":"simonwaldherr","open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":"SimonWaldherr","issuehunt":null,"otechie":null,"custom":["https://flattr.com/@SimonWaldherr"]}},"created_at":"2013-04-15T22:42:13.000Z","updated_at":"2025-03-30T08:05:00.000Z","dependencies_parsed_at":"2022-08-06T20:15:27.643Z","dependency_job_id":"e3041173-2893-445e-b13d-847de6eb3929","html_url":"https://github.com/SimonWaldherr/golang-examples","commit_stats":{"total_commits":210,"total_committers":52,"mean_commits":4.038461538461538,"dds":0.3380952380952381,"last_synced_commit":"e47c72cc35d6e30b6a0a2f6565556d7ba27dd621"},"previous_names":[],"tags_count":33,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SimonWaldherr%2Fgolang-examples","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SimonWaldherr%2Fgolang-examples/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SimonWaldherr%2Fgolang-examples/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SimonWaldherr%2Fgolang-examples/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SimonWaldherr","download_url":"https://codeload.github.com/SimonWaldherr/golang-examples/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248313181,"owners_count":21082815,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["education","examples","go","golang","hacktoberfest","hacktoberfest-accepted","howto","learning","programming-language"],"created_at":"2024-09-26T02:03:59.544Z","updated_at":"2025-04-10T23:22:08.823Z","avatar_url":"https://github.com/SimonWaldherr.png","language":"Go","funding_links":["https://github.com/sponsors/simonwaldherr","https://patreon.com/simonwaldherr","https://liberapay.com/SimonWaldherr","https://flattr.com/@SimonWaldherr"],"categories":[],"sub_categories":[],"readme":"# Go Examples\n\n[![DOI](https://zenodo.org/badge/9459712.svg)](https://zenodo.org/badge/latestdoi/9459712) \n[![Go Report Card](https://goreportcard.com/badge/github.com/simonwaldherr/golang-examples)](https://goreportcard.com/report/github.com/simonwaldherr/golang-examples) \n[![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://opensource.org/licenses/MIT)  \n\nUse the [online live editor with Golang support](https://simonwaldherr.github.io/golang-examples/). Edit and run the examples directly in your browser:\n[![SimonWaldherr/golang-benchmarks Online Editor](https://simonwaldherr.github.io/golang-examples/golang-examples.png)](https://simonwaldherr.github.io/golang-examples/)  \n\n\n\nIf you liked this project, you may also like  \nmy [golang-benchmarks](https://github.com/SimonWaldherr/golang-benchmarks) repository:  \n[![SimonWaldherr/golang-benchmarks - GitHub](https://gh-card.dev/repos/SimonWaldherr/golang-benchmarks.svg?fullname)](https://github.com/SimonWaldherr/golang-benchmarks)  \nmy [gotools](https://github.com/SimonWaldherr/gotools) repository:  \n[![SimonWaldherr/gotools - GitHub](https://gh-card.dev/repos/SimonWaldherr/gotools.svg?fullname)](https://github.com/SimonWaldherr/gotools)  \nmy [sql-examples](https://github.com/SimonWaldherr/sql-examples) repository:  \n[![SimonWaldherr/sql-examples - GitHub](https://gh-card.dev/repos/SimonWaldherr/sql-examples.svg?fullname)](https://github.com/SimonWaldherr/sql-examples)  \nmy [rp2040-examples](https://github.com/SimonWaldherr/rp2040-examples) repository:  \n[![SimonWaldherr/rp2040-examples - GitHub](https://gh-card.dev/repos/SimonWaldherr/rp2040-examples.svg?fullname)](https://github.com/SimonWaldherr/rp2040-examples)  \nor my [rpi-examples](https://github.com/SimonWaldherr/rpi-examples) repository:  \n[![SimonWaldherr/rpi-examples - GitHub](https://gh-card.dev/repos/SimonWaldherr/rpi-examples.svg?fullname)](https://github.com/SimonWaldherr/rpi-examples)\n\n## About\n\nThese examples explain the basics of Golang. There will be more examples from time to time.\n\nif you like, feel free to add more Golang examples. Many thanks to all [contributors](https://github.com/SimonWaldherr/golang-examples/graphs/contributors).\n\n## Install go(lang)\n\nwith [homebrew](http://mxcl.github.io/homebrew/):\n\n```Shell\nsudo brew install go\n```\n\nwith [apt](http://packages.qa.debian.org/a/apt.html)-get:\n\n```Shell\nsudo apt-get install golang\n```\n\n[install Golang manually](https://golang.org/doc/install)\nor\n[compile it yourself](https://golang.org/doc/install/source)\n\n## Examples\n\nThe examples are divided into three levels of difficulty. The [Beginner](https://github.com/SimonWaldherr/golang-examples#beginner) section contains very easy examples, starting with **Hello World** but also containing a few easy algorithms. The [Advanced](https://github.com/SimonWaldherr/golang-examples#advanced) section uses more complicated features of Golang. Finally, the [Expert](https://github.com/SimonWaldherr/golang-examples#expert) section contains applications like telnet-clients or http-server (even with SSL).\nIf you want even more Golang examples, you can take a look at my other go repositories at GitHub:\n\n* [golang-benchmarks](https://github.com/SimonWaldherr/golang-benchmarks) shows how to benchmark the execution time of Golang functions\n* [GolangSortingVisualization](https://github.com/SimonWaldherr/GolangSortingVisualization) visualizes various sorting algorithms on the terminal or as gif\n* [golang-minigames](https://github.com/SimonWaldherr/golang-minigames) currently only contains a snake clone\n* [bbmandelbrot.go](https://github.com/SimonWaldherr/bbmandelbrot.go) calculates a [Mandelbrot Fractal](https://en.wikipedia.org/wiki/Mandelbrot_set) and saves it as PNG\n* [golibs](https://github.com/SimonWaldherr/golibs) contains various Go packages (e.g. math, converter, stack, cli, ...)\n* [fsagent](https://github.com/SimonWaldherr/fsagent) watch a folder for new or modified files and do something\n* [cgol.go](https://github.com/SimonWaldherr/cgol.go) is [Conway's](https://en.wikipedia.org/wiki/John_Horton_Conway) [Game of Life](https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life) in [Golang](https://en.wikipedia.org/wiki/Go_(programming_language))\n* [micromarkdownGo](https://github.com/SimonWaldherr/micromarkdownGo) converts markdown to html (via regular expression)\n* [wikiGo](https://github.com/SimonWaldherr/wikiGo) is a wiki software in Go\n* [WorkingTimeMeasurementSystem](https://github.com/SimonWaldherr/WorkingTimeMeasurementSystem) demonstrates how to create a simple time tracking system using Golang and SQLite\n* [zplgfa](https://github.com/SimonWaldherr/zplgfa) is an image converter to print pictures on zpl compatible labels\n* [...](https://github.com/search?utf8=✓\u0026q=user%3Asimonwaldherr\u0026type=Repositories\u0026ref=advsearch\u0026l=Go)\n\nAll of them are published as free and open source software.\n\nIf all of this is even not enough for you, you can take a look at the following websites:\n\n* [tour.golang.org](https://tour.golang.org/)\n* [Go by example](https://gobyexample.com/)\n* [Golang Book](http://www.golang-book.com/)\n* [Go-Learn](https://github.com/skippednote/Go-Learn)\n\n### Beginner\n\nTo execute a **Golang** program, write ```go run``` at the cli followed by the name of the file.  \nYou also can convert the file to a binary executable program by the command ```go build```.  \nIf you know ```#!```, also known as [Shebang](https://en.wikipedia.org/wiki/Shebang_(Unix)), there is an equivalent for go: ```//usr/bin/env go run $0 $@ ; exit```  \n\nPrint Hello World with comments ([Golang Playground](https://play.golang.org/p/PiUVBrRB9AR))\n\n```Shell\ngo run HelloWorld.go\n```\n\nPrint Hello World with comments (shebang version)\n\n```Shell\n./HelloWorldShebang.go\n```\n\nDeclare variables and print them ([Golang Playground](https://play.golang.org/p/O3_FWH2IQ75))\n\n```Shell\ngo run var.go\n```\n\nVarious ways (and styles) to print variables ([Golang Playground](https://play.golang.org/p/QH05DN_CeJU))\n\n```Shell\ngo run printf.go\n```\n\nIf statement in Golang ([Golang Playground](https://play.golang.org/p/rIpps0zUl1N))\n\n```Shell\ngo run if.go Hello\n```\n\nDeclare array and print its items ([Golang Playground](https://play.golang.org/p/jqseOd76Dqk))\n\n```Shell\ngo run array.go\n```\n\nDeclare your own functions ([Golang Playground](https://play.golang.org/p/9L0-F76gK0D))\n\n```Shell\ngo run function.go\n```\n\nDo something multiple times ([Golang Playground](https://play.golang.org/p/5G3Ek89eKCH))\n\n```Shell\ngo run for.go\n```\n\nRead via cli provided input data ([Golang Playground](https://play.golang.org/p/oUZ97D0Kg-_O))\n\n```Shell\ngo run args.go string string2\n```\n\nRead via cli provided input data ([Golang Playground](https://play.golang.org/p/4IjMGLZBmue))\n\n```Shell\ngo run input.go\n```\n\nOr scan for it ([Golang Playground](https://play.golang.org/p/rZ6clB9Z9Zu))\n\n```Shell\ngo run scan.go\n```\n\nRead named argument input data ([Golang Playground](https://play.golang.org/p/i7BXecoK_wZ))\n\n```Shell\ngo run flag.go\n```\n\nReturn the *working directory* ([Golang Playground](https://play.golang.org/p/Ijx04bm8r8s))\n\n```Shell\ngo run dir.go\n```\n\nReturn the current time/date in various formats ([Golang Playground](https://play.golang.org/p/tPbn_CbHYYw))\n\n```Shell\ngo run time.go\n```\n\nReturn pseudo random integer values ([Golang Playground](https://play.golang.org/p/PapALcxQkpN))\n\n```Shell\ngo run random.go\n```\n\nConcat strings in two different ways ([Golang Playground](https://play.golang.org/p/sBD_-peBzm5))\n\n```Shell\ngo run cat.go\n```\n\nModulo operation finds the remainder of division ([Golang Playground](https://play.golang.org/p/mYcueUHX1XL))\n\n```Shell\ngo run modulo.go\n```\n\nSplit a string by another string and make an array from the result ([Golang Playground](https://play.golang.org/p/VYP9tVMcbPW))\n\n```Shell\ngo run split.go\n```\n\nAn example implementation of the Ackermann function ([Golang Playground](https://play.golang.org/p/OjNgscvZhZT))\n\n```Shell\ngo run ackermann.go\n```\n\nAn example implementation of the Euclidean algorithm ([Golang Playground](https://play.golang.org/p/b1HWeUyk4Uf))\n\n```Shell\ngo run euklid.go\n```\n\nSubmit a function as argument ([Golang Playground](https://play.golang.org/p/fy0_S3J29_2))\n\n```Shell\ngo run functioncallback.go\n```\n\nA function returned by a function ([Golang Playground](https://play.golang.org/p/earjKURMsPp))\n\n```Shell\ngo run functionclosure.go\n```\n\nA function with an unknown amount of inputs (variadic function) ([Golang Playground](https://play.golang.org/p/s4Fvj8voh3Y))\n\n```Shell\ngo run functionvariadic.go\n```\n\nEmpty interface as argument (You Don't Know Type) ([Golang Playground](https://play.golang.org/p/D7OPshRATt_e))\n\n```Shell\ngo run interface.go\n```\n\nExecute Shell/Bash commands and print its output values ([Golang Playground](https://play.golang.org/p/4Z-wLSkPJll))\n\n```Shell\ngo run shell.go\n```\n\nMake structs (objects) which have functions ([Golang Playground](https://play.golang.org/p/t-082xlTu2t))\n\n```Shell\ngo run oop.go\n```\n\nDependency injection for easier testing\n\n```Shell\ncd beginner/di\ngo test\n```\n\nHashing (md5, sha) in go ([Golang Playground](https://play.golang.org/p/fB2Y2MV7zt3))\n\n```Shell\ngo run hashing.go\n```\n\n### Advanced\n\nBenchmarking example (using JSON marshal and unmarshal for the sample) ([Golang Playground](https://play.golang.org/p/80Tcnkb301J)) \nFrom the root directory (`$GOPATH/github.com/SimonWaldherr/golang-examples`), run this command:\n\n```Shell\ngo test -bench=. -benchmem advanced/json_bench/main_test.go\n```\n\nMake pipe-able unix applications with os.Stdin ([Golang Playground](https://play.golang.org/p/NqrUOfBmJtt))\n\n```Shell\ngo run pipe.go\n```\n\nAES-GCM encryption example ([Golang Playground](https://play.golang.org/p/ujfs6s5JZ-P))\n\n```Shell\ngo run aesgcm.go\n```\n\nBcrypt hashing example ([Golang Playground](https://play.golang.org/p/9R7oS56Od6H)) \nPlease install package golang.org/x/crypto/bcrypt before run this file by running `go get golang.org/x/crypto/bcrypt`\n\n```Shell\ngo run bcrypt.go\n```\n\nSearch element is exist in arrays or not ([Golang Playground](https://play.golang.org/p/1gVa9Jgk6vg))\n\n```Shell\ngo run in_array.go\n```\n\nCalculate triangles ([Golang Playground](https://play.golang.org/p/l8ehuAWZitv))\n\n```Shell\ngo run pythagoras.go (float|?) (float|?) (float|?)\n```\n\nRead from stdin (but don't wait for the enter key)\n\n```Shell\ngo run getchar.go\n```\n\nWait and sleep ([Golang Playground](https://play.golang.org/p/qGec1g7rTHC))\n\n```Shell\ngo run wait.go\n```\n\nLast in - first out - example (Pop and push in Golang) ([Golang Playground](https://play.golang.org/p/TekltztwUfE))\n\n```Shell\ngo run lifo.go\n```\n\nSplit a string via regular expression and make an array from the result ([Golang Playground](https://play.golang.org/p/sWFDPMyF-wD))\n\n```Shell\ngo run regex.go\n```\n\nMore advanced regex (with time and dates) ([Golang Playground](https://play.golang.org/p/u1SdhFEwRch))\n\n```Shell\ngo run regex2.go\n```\n\nUse my [golibs regex package](https://github.com/SimonWaldherr/golibs#regex-----) and have fun ([Golang Playground](https://play.golang.org/p/1RxtOxL0nQo))\n\n```Shell\ngo run regex3.go\n```\n\nCalculate and print the fibonacci numbers ([Golang Playground](https://play.golang.org/p/BbjQ_ohw0m1))\n\n```Shell\ngo run fibonacci.go\n```\n\nCalculate and print the requested (32th) prime number ([Golang Playground](https://play.golang.org/p/fB25PQlVWu4))\n\n```Shell\ngo run prime.go 32\n```\n\nDo things with numbers, strings and switch-cases ([Golang Playground](https://play.golang.org/p/MvKDvR_wzlQ))\n\n```Shell\ngo run numbers.go\n```\n\nUse a template to create and fill documents (this example uses [LaTeX](https://www.latex-project.org)) ([Golang Playground](https://play.golang.org/p/riy6SU21alH))\n\n```Shell\ngo run template.go\npdflatex -interaction=nonstopmode template_latex.tex\n```\n\nStart a ticker (do things periodically) \n\n```Shell\ngo run ticker.go\n```\n\nDo something in case of a timeout ([Golang Playground](https://play.golang.org/p/Qtu62LaC3-q))\n\n```Shell\ngo run timeout.go\n```\n\nConvert go object to json string ([Golang Playground](https://play.golang.org/p/s1onU7jpP91))\n\n```Shell\ngo run json.go\n```\n\nRun unix/shell commands in go apps\n\n```Shell\ngo run exec.go\n```\n\nCompress by pipe\n\n```Shell\ngo run compress.go\n```\n\nCompress by file\n\n```Shell\ngo run compress2.go\n```\n\nParse CSV ([Golang Playground](https://play.golang.org/p/plVmXW8TB32))\n\n```Shell\ngo run csv.go\n```\n\nConvert CSV to a Markdown table ([Golang Playground](https://play.golang.org/p/HTobb_U9JQt))\n\n```Shell\ngo run csv2md.go\n```\n\nParse a XML string into a Struct with undefined Fields ([Golang Playground](https://play.golang.org/p/6LuNPcaeagS))\n\n```Shell\ngo run xml.go\n```\n\nRun a self killing app\n\n```Shell\ngo run suicide.go\n```\n\nGoCV : hello video\n\n```Shell\ngo run hello_video.go\n```\n\nGoCV : face detection\n\n```Shell\ngo run face_detect.go 0 model/haarcascade_frontalface_default.xml\n```\n\nRun the example for generic ([Golang Playground](https://go.dev/play/p/586EEI0ZAEe))\n\n```Shell\ngo run generic.go\n```\n\n### Expert\n\nCalculate π with go (leibniz, euler and prime are running until you stop it via CTRL+C)\n\n```Shell\ngo run pi2go.go leibniz\ngo run pi2go.go euler\ngo run pi2go.go prime\n```\n\nCalculate π with go - same as above - but with live output (based on [gcurses](https://godoc.org/github.com/SimonWaldherr/golibs/gcurses))\n\n```Shell\ngo run pi2go-live.go leibniz\ngo run pi2go-live.go euler\ngo run pi2go-live.go prime\n```\n\n\nList files in working directory\n\n```Shell\ngo run explorer.go\n```\n\nrun assembly code from golang\n\n```Shell\ngo run assembly.go\n```\n\nrun C code from golang \n\n```Shell\ngo run cgo.go\n```\n\ngenerate Go code with golang templates\n\n```Shell\ngo run codegen.go\n```\n\nConvert from rgb to hsl ([Golang Playground](https://play.golang.org/p/UuX27PhA0Zx))\n\n```Shell\ngo run color.go\n```\n\nTelnet with Golang\n\n```Shell\ngo run telnet.go\n```\n\nThe smallest Golang http server\n\n```Shell\ngo run httpd.go\n```\n\nSecure Golang http server\n\n```Shell\ngo run httpsd.go\n```\n\nThe smallest Golang http proxy\n\n```Shell\ngo run proxy.go\n```\n\nRead and write cookies\n\n```Shell\ngo run cookies.go\n```\n\nDemonstrate the power of multithreading / parallel computing\nyou have to set GOMAXPROCS to something greater than 1 to see any effect\n\n```Shell\nexport GOMAXPROCS=8\ntime go run parallel.go true\ntime go run parallel.go false\n```\n\nA dynamic amount of channels\n\n```go\ntime go run dynparallel.go 8\n```\n\nRun the compiler and comment each line which contains an error\n\n```Shell\ngo build gocomment.go\n./gocomment go-app.go\n```\n\nConvert a image to a grayscale and to a color inverted image\n\n```Shell\ngo run image.go\n```\n\nGenerate an image with three colored circles (with intersection)\n\n```Shell\ngo run image2.go\n```\n\nGenerate an image representing the Mandelbrot fractal\n\n```Shell\ngo run image3.go\n```\n\nSql (sqlite) Golang example  \nmaybe you also wanna take a look at my [sql-examples](https://github.com/SimonWaldherr/sql-examples)-project\n\n```Shell\ngo run sqlite.go insert test\ngo run sqlite.go select\n```\n\nPublic-key/asymmetric cryptography signing and validating\n\n```Shell\ngo run ppk-crypto.go\n```\n\nCommand Line Arguments Golang Example\nWe 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\n\n```Shell\ngo run command_line_arguments.go\ngo run command_line_arguments.go -env=qa -consumer=true\n```\n\nCron Golang Example\nWe can trigger a function at a particular time through cron \n\n```Shell\ngo run cron.go\n```\n\nMap Golang Example\nHash Map standard functions in golang \n\n```Shell\ngo run map.go\n```\n\n### TinyGo\n\nYou can even use Go on microcontrollers, the keyword here is [TinyGo](https://tinygo.org/), a go compiler specially developed for SBCs and MCUs.  \nIf you want to blink the LED of your Raspberry Pi Pico, try this: \n\n```Shell\ntinygo build -o firmware.uf2 -target=pico ./tinygo/blink.go\n```\n\nand then upload it to the pico.\n\n## Compile\n\nOne 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.\nIf 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.\n\n## License\n\nCopyright © 2024 Simon Waldherr\nDual-licensed. See the [LICENSE](https://github.com/SimonWaldherr/golang-examples/blob/master/LICENSE) file for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimonwaldherr%2Fgolang-examples","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsimonwaldherr%2Fgolang-examples","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimonwaldherr%2Fgolang-examples/lists"}