Go using AF_UNIX

You can use Unix Domain Socket aka AF_UNIX for your interprocess communication. Previously, It was only available in a Linux/Unix operating system until Microsoft added it in Microsoft Windows in the beginning of Insider build 17063. It offers better throughput and improved security package main import ( "log" "net" "net/http" "os" "os/signal" "syscall" ) func main() { socketPath := "uds.sock" socket, err := net.Listen("unix", socketPath) if err != nil { log....

February 14, 2024
set-http-request-body-size-in-go.webp

Set HTTP Request Body Size in Go

Use http.MaxBytesReader to limit and control the size HTTP request body. This is a good practice to prevent abuse and save bandwidth. In the example below, I set a 20 bytes HTTP request body size limit. package main import ( "github.com/julienschmidt/httprouter" "io" "log" "net/http" "time" ) func main() { router := httprouter.New() router.HandlerFunc(http.MethodPost, "/", func(w http.ResponseWriter, r *http.Request) { buffer, err := io.ReadAll(http.MaxBytesReader(w, r.Body, 20)) defer r.Body.Close() if err != nil { w....

October 23, 2023 · John Pili
golang-with-recaptcha.webp

Golang with reCAPTCHA

Google’s reCAPTCHA is one of the tool we can use to stop malicious internet bots from abusing our web applications. It comes in two versions, reCAPTCHA v2 and v3. Version 3 uses a score-based method with no user interaction. Version 2 uses a checkbox that will require users to answer a question. In this tutorial we will focus on reCAPTCHA v2. Prerequisite This tutorial requires the following: Registered Google webmaster account Domain registered property in Google webmaster Created Google reCAPTCHA account – https://www....

December 18, 2022 · John Pili
embed-resources-in-go.webp

Embed Resources in Go

The go:embed feature was introduce in Go 1.16. It lets you embed resources into the compiled application. Prior to version 1.16, developers uses external tooling to do this. With go:embed you can embed images, webpages, email templates, predefined SQL statements or an entire directory. That’s neat! Usage Examples Embedding a text file in a string //go:embed version.txt var version string Embedding a binary file //go:embed product-catalog.pdf var catalog []byte Embedding a directory filesystem //go:embed template/* var templateFS fs....

December 18, 2022
Remove Source Path From Go's Panic Stack Trace

Remove Source Path From Go's Panic Stack Trace

I would like to share the Golang’s build flag to remove the source path (GOPATH) from panic stack trace output. In production environments or commercial projects it is sometimes not ideal to display the source path because of privacy, security or other reasons. Below is an example of a stack trace output that reveals the GOPATH location which is located inside the developer’s home directory. In this case /home/johnpili/go/ panic: Aw, snap goroutine 1 [running]: main....

February 19, 2021 · John Pili
Generate text to image in Go

Generate text to image in Go

In this blog post, I’ll share how to generate text to image in Go programming language (Golang). I have a previous and similar blog post using Python. You can check that post here I created this application to generate images of my Linux configuration files or source code snippets and share it via WhatsApp or other messaging platforms. Another reason is to generate featured images for my social media posts in Twitter, Facebook or LinkedIn....

February 12, 2021 · John Pili
golang-linux-daemon.webp

Golang Linux Daemon

You build your first Golang web application and running in a remote server via SSH. The problem with that is once the SSH session is terminated it also kill any running programs associated with that SSH session. Using nohup solves this problem but I think this is okay during development and testing phase. A better way to deploy your Golang application into a Linux production environment is create a systemd entry making it as a daemon program....

July 28, 2020 · John Pili
randomly-create-a-time-sleep-in-golang.webp

Randomly create a time.Sleep in Golang

To randomly create a time.Sleep in Golang you can use the code snippet below. You may want to simulate a load in your web server and have an arbitrary seconds or minutes before getting the reply. package main import ( "log" "math/rand" "time" ) func main() { rand.Seed(time.Now().UnixNano()) for i := 0; i < 15; i++ { delta := rand.Intn(6 + 1) // randomly generates numbers 1 to 6 time.Sleep(time.Duration(delta) * time....

July 2, 2020 · John Pili
golang-sqlite-simple-example.webp

Golang SQLite Simple Example

In this post, I will show you a simple example how to use SQLite in Go (Golang). SQLite is one of the popular embedded, file-based database in the market used by companies like Apple, Airbus, Google, Skype, Autodesk and Dropbox. You can check out the list of well-know SQLite user in this link https://www.sqlite.org/famous.html Requirements Knowledge in Terminal or command prompt An installed Go 1.19 or latest Knowledge in compiling Go codes Visual Studio Code (Optional) Let’s begin We will use a library from https://github....

February 26, 2020 · John Pili
How to parse JSON data without struct in Go

How to parse JSON data without struct in Golang

In using Golang, we sometimes need to parse a JSON data without knowing or specifying a concrete type struct. We can do this by converting (json.Unmarshal) JSON data into an interface{}. We can then use it like a map. Accessing it like for example m[“username”].(string) Below is an example application that converts a JSON string into an interface{}. The statements from line 18 to 23 implements the JSON unmarshall without a concrete struct....

January 2, 2020 · John Pili