{"id":15035426,"url":"https://github.com/jeamon/gosnippets","last_synced_at":"2026-02-24T01:10:25.186Z","repository":{"id":43170229,"uuid":"282526992","full_name":"jeamon/gosnippets","owner":"jeamon","description":"some real-world code snippets into golang used into some of my several back-ends services and console-based tools.","archived":false,"fork":false,"pushed_at":"2024-09-08T12:35:04.000Z","size":61,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-08-20T01:07:58.915Z","etag":null,"topics":["backend-development","go","golang","golang-examples","http-client","http-server","snippets","system-programming","web-application"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jeamon.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-07-25T21:22:17.000Z","updated_at":"2024-09-08T12:35:08.000Z","dependencies_parsed_at":"2025-02-21T07:31:18.297Z","dependency_job_id":null,"html_url":"https://github.com/jeamon/gosnippets","commit_stats":null,"previous_names":["jeamon/gosnippets","jeamon/useful-code-snippets-in-golang"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/jeamon/gosnippets","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jeamon%2Fgosnippets","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jeamon%2Fgosnippets/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jeamon%2Fgosnippets/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jeamon%2Fgosnippets/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jeamon","download_url":"https://codeload.github.com/jeamon/gosnippets/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jeamon%2Fgosnippets/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":281212374,"owners_count":26462303,"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","status":"online","status_checked_at":"2025-10-27T02:00:05.855Z","response_time":61,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["backend-development","go","golang","golang-examples","http-client","http-server","snippets","system-programming","web-application"],"created_at":"2024-09-24T20:28:38.003Z","updated_at":"2025-10-27T04:19:26.434Z","avatar_url":"https://github.com/jeamon.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# go-snippets\nThis repository  contains some real world code snippets into golang. Those come from some of my mutiple go-based projects.\nfeel free to learn from them. For any help regarding this repo - contact me at https://blog.cloudmentor-scale.com/contact\n\n* ### [dual-servers-http-https-with-custom-tls.go](https://github.com/jeamon/gosnippets/blob/master/dual-servers-http-https-with-custom-tls.go)\n\nThis is a simple go program that demonstrates how to spin up HTTP \u0026 HTTPS servers at the same time\nand force all HTTP traffic to be redirected to HTTPS while configurting HSTS. All done with customized \nTLS configurations. You will find some useful openssl commands to generate your self-signed certificates.\nThis program expects to load key and self-signed certificate from child directory named \"certificates\".\n\n*commands examples to generate certificates with OpenSSL 1.1.1c*\n```\n[+] RSA-Based keys - Self Signed Certificate Generation (key - recommendation key ≥ 2048) \n\n1/ openssl req -x509 -nodes -newkey rsa:4096 -keyout server.rsa.key -out server.rsa.crt -days 365\n\n[+] ECDSA-Based keys - Self Signed Certificate Generation (key - recommendation key ≥ secp384r1)**\n\n1/ To list ECDSA supported curves use following command : openssl ecparam -list_curves  \n\n2/ openssl req -new -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -x509 -nodes -days 365 -out server.ecdsa.crt -keyout server.ecdsa.key  \n\n3/ openssl req -x509 -nodes -newkey ec:secp384r1 -keyout server.ecdsa.key -out server.ecdsa.crt -days 365  \n\n4/ openssl req -x509 -nodes -newkey ec:\u003c(openssl ecparam -name secp384r1) -keyout server.ecdsa.key -out server.ecdsa.crt -days 365  \n\nTLS   : Transport Layer Security\nECDHE : Ephemeral Elliptic Curve Diffie-Hellman\nRSA   : Rivest–Shamir–Adleman\nECDSA : Elliptic Curve Digital Signature Algorithm\nGCM   : Galois Counter Mode\nSHA   : Secure Hash Algorithm\n\n```\n\n* ### [demo-native-http-server.go](https://github.com/jeamon/gosnippets/blob/master/demo-native-http-server.go)\n\nThis is another go web development program that constructs a HTTP server with a middleware in charge of generating \u0026 assigning an ID\nto each request that comes in. It shows up the useage of the built-in shutdown feature (available from 1.8+). This is achieved\nwith two separate goroutines (one which handles exit system calls - another waiting for a notification in order to call the shutdown)\nand a timeout-based context. Finally, you'll find a simple way to associate a customized logger to the server. Below are some outputs.\n\n```\n\n[+] Outputs on server console.\n\n[ 2:11:23] {nxos-geek}:~$ go run http-web.go\n2021/08/26 02:15:45 web server is starting ...\n2021/08/26 02:15:48 received new request  [id: 9c4565b075528b7f] - logging ...\n[request] 2021/08/26 02:15:48 [id: 9c4565b075528b7f] [ip: 127.0.0.1:63448] [method: GET] [url: /] [browser: curl/7.55.1]\n2021/08/26 02:15:48 completed new request [id: 9c4565b075528b7f] - thanks ...\n2021/08/26 02:15:52 received new request  [id: b56b0ea8c72dfcd6] - logging ...\n[request] 2021/08/26 02:15:52 [id: b56b0ea8c72dfcd6] [ip: 127.0.0.1:63451] [method: GET] [url: /welcome] [browser: curl/7.55.1]\n2021/08/26 02:15:52 completed new request [id: b56b0ea8c72dfcd6] - thanks ...\n2021/08/26 02:15:57 received new request  [id: 7f9e1905baf425b6] - logging ...\n[request] 2021/08/26 02:15:57 [id: 7f9e1905baf425b6] [ip: 127.0.0.1:63461] [method: GET] [url: /] [browser: curl/7.55.1]\n2021/08/26 02:15:57 completed new request [id: 7f9e1905baf425b6] - thanks ...\n2021/08/26 02:16:01 received new request  [id: 17ba4ecf3741bfb0] - logging ...\n[request] 2021/08/26 02:16:01 [id: 17ba4ecf3741bfb0] [ip: 127.0.0.1:63467] [method: GET] [url: /welcome] [browser: curl/7.55.1]\n2021/08/26 02:16:01 completed new request [id: 17ba4ecf3741bfb0] - thanks ...\nreceived signal type:  interrupt\nexit command received. exiting ...\n2021/08/26 02:16:05 shutting down the web server ... please wait for 45 secs max\n\n[ 2:16:05] {nxos-geek}:~$\n\n\n[+] Outputs on client console.\n\n[ 2:11:18] {nxos-geek}:~$ curl localhost:8080/\n404 page not found\n\n[ 2:15:48] {nxos-geek}:~$ curl localhost:8080/welcome\nsimple plain welcome.\n[ 2:15:52] {nxos-geek}:~$ curl localhost:8080/\n404 page not found\n\n[ 2:15:57] {nxos-geek}:~$ curl localhost:8080/welcome\nsimple plain welcome.\n[ 2:16:01] {nxos-geek}:~$\n\n``` \n\n* ### [contextual-command-executor.go](https://github.com/jeamon/gosnippets/blob/master/multiple-commands-executor/main.go)\n\nThis cross-platform tool allows to execute multiple commands from shell with possibility to specify single execution timeout for all of the commands.\nEach command output will be streaming to a unique file named with command id (order number into the list) suffixed by the hour minutes and seconds of \nprogram start time. Be aware that all these output files will be saved into a unique daily folder. If the daily folder doesn't not exist it will be created.\nIn event of creation failure, the program aborts it execution. The timeout and execution cancellation is achieved with builtin CommandContext feature available \nfrom go v1.7. Please find below some details on how to use this tool.\n\n```\nUsage:\n    \n    Please in case you want to define a timeout value - it should come just after the program name\n    and before all the tasks command to be executed. Notice that these tasks should be double quoted.\n    Be aware that for demonstration purpose, if no tasks are provided - default tasks will be used.\n    \n    command-executor [-timeout \u003cexecution-deadline-in-seconds\u003e] \"task-one\" \"task-two\" . . . \"task-three\"\n\n    Examples:\n\n    --- on Windows\n\n    go run contextual-command-executor.go \"tasklist\" \"ipconfig /all\" \"systeminfo\"\n    go run contextual-command-executor.go -timeout 120 \"tasklist\" \"ipconfig /all\" \"systeminfo\"\n\n    --- on Linux\n\n    go run contextual-command-executor.go \"ps\" \"ifconfig\" \"cat /proc/cpuinfo\" \"ping 8.8.8.8\"\n    go run contextual-command-executor.go -timeout 120 \"ps\" \"ifconfig\" \"cat /proc/cpuinfo\" \"ping 8.8.8.8\"\n\n    --- help or version checkings\n\n    go run contextual-command-executor.go --help\n    go run contextual-command-executor.go --version\n    go run contextual-command-executor.go -h\n    go run contextual-command-executor.go -v\n\n```\n\n\n* ### [dynamic-webapp-spam-loader.go](https://github.com/jeamon/gosnippets/blob/master/auto-spam-words-loader/main.go)\n\nThis is a short routine that runs in background as goroutine every customized hours which will load a list of your defined spam words and \nstore them into the program memory for usage. It loads the spam file content only if the size of the file has changed or if the latest\nmodification date attribute changed. This means we keep track of these two values. Updating the in-memory list store uses RW mutex to\nfacilitates concurent reading access. There is a code snippet which demonstrates how you can use this spam loader routine into your code.\nThe idea is simple. Each time a user submit a message, you need to check if the message contains one of your spam words before proceding. \n\n```go\n\n// contact submission message format.\ntype Message struct {\n\tFullName string\n\tEmail string\n\tSubject string\n\tContent string\n\tErrors map[string]string\n}\n\n// check if subject or content is suspicious.\nfunc (msg *Message) isSpamMessage() bool {\n\t// acquire the lock and ensure its release.\n\taddSpamMutex.RLock()\n\tdefer addSpamMutex.RUnlock()\n\t// loop over the spam words list and stop once there is a hit\n\tfor _, spam := range spamWords {\n\t\tif strings.Contains(msg.Content, spam) || strings.Contains(msg.Subject, spam) {\n\t\t\treturn true\n\t\t}\n\t}\n\n\treturn false\n}\n\n// snipped to insert into the contact handler and send fake confirmation for spam message.\nif msg.isSpamMessage() {\n\t// routine to handle goes here\n\t// you can ignore user message or send fake confirmation\n}\n\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjeamon%2Fgosnippets","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjeamon%2Fgosnippets","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjeamon%2Fgosnippets/lists"}