How to Build a QR Code Generator Service in Go: A Step-by-Step Guide

QR codes have become an essential tool for bridging the physical and digital worlds. From enabling quick logins on smart TVs to sharing promotional content on printed cards, QR codes are versatile and easy to use. In this blog post, I’ll walk you through how to build your own QR code generator service using Go. We’ll create a simple web application that takes a URL as input, generates a QR code, and displays it to the user....

Mastering Go Application Design: Make an API Request With the Default Go HTTP Client

Executing HTTP requests in golang can be used to create an api integration and automate workflows. The standard library “net/http” package can be used to create a request object and the DefaultClient can be used to execute the request. We can expand this basic example to create complex interactions between backend services communicating over HTTP APIs. Playground: https://go.dev/play/p/AGV2OyqN-a6 package main import ( "io" "net/http" ) func main() { // create a new request req, err := http....

Mastering Go Application Design: Building a Basic CLI from Scratch with Only the Standard Library

Golang has built in support for creating CLI applications that accept command line flags and arguments. The “flag” package has convenient methods for reading in options as typed variables, it also supports creating default help text. CLI applications are useful for creating developer tooling, especially when you can import critical parts of the codebase into a simple cli. Playground: https://go.dev/play/p/UHuXCNgQB8j package main import "flag" var ( // define the command line arguments customerID *int = flag....

Mastering Go Application Design: Simplify Complex Systems With a Diagram

Diagraming your applications can be a useful exercise. It can give you a chance to think clearly about what already exists, what you plan to create and how it will work. Some of the best systems I’ve designed were complete mysteries to me, then I took time to diagram what already existed and it helped me see what needed to be created next. Diagrams can also be extremely valuable documentation, even if they are slightly out of date....

Mastering Go Application Design: Protobuf vs JSON Encoding

Protobuf and JSON are two popular methods for structuring data for efficient storage and transfer. Protobuf is more efficient than JSON in terms of byte array size, but it takes longer to convert an object to a byte array and changes to the message structure require code generation. When Protobuf is used to replace JSON at large scale, it can save massive amounts of bandwidth. Example Github Repository here Protobuf file Marshal Lets start with some go code with the protos already defined and working....

Mastering Go Application Design: HTTP Path Parameters (Part 2)

Part 1 , Part 2 HTTP Api’s often use the url path to identify resource name, id and possibly an action. These parts of the URL path are divided by the / character. Here is an example of using path parameters to implement a key value store where data is saved using the following request GET /set/$key/$value and retrieved using the request GET /get/$key using only the standard library. Go Playground: https://go....

Mastering Go Application Design: HTTP Servers (Part 1)

Part 1 , Part 2 Go is an excellent choice for writing your next http service. It supports multithreaded request handling with the standard library and many other convenient features that makes creating a new application a breeze. Here is an example of a http service that returns the current time as json. Playground: https://go.dev/play/p/gZGKLEhHkPR package main import ( "encoding/json" "log" "net/http" "time" ) // Response is a simple json response // that will be returned by the http server // the json tags are used to specify the // json keys type Response struct { Status string `json:"status"` // json key is "status" Time string `json:"time"` // json key is "time" } // this is a simple http server that returns a json response func main() { // create a new http serve mux // the serve mux is used to register http handlers // for different paths mux := http....

Mastering Go Concurrency: Running Background Tasks with Goroutines (Part 5)

Part 1 , Part 2 , Part 3 , Part 4 , Part 5 Goroutines can be used to start independent long running task. These tasks can be entire microservices, which is very useful for creating a single application with multiple system responsibilities. The select statement will block and wait for a channel to be ready for reading, then after reading from the channel the code will be executed. Playground: https://go....

Mastering Concurrency in Go: Decoupling Data Transfer With Buffered Channels (Part 4)

Part 1 , Part 2 , Part 3 , Part 4 , Part 5 Buffered Channels in Go are similar to unbuffered channels, but they allow a writer to write without blocking if the channel is not full, which can be useful for concurrent data processing where the reader needs to be decoupled. However, using buffered channels can consume memory even when the buffer is empty, so they should be used with care....

Concurrency Limiting in Go Maximizing Application Efficiency and Resource Utilization

Playground , Repo Why do you need concurrency limiting? Concurrency limiting in your go application might be necessary to limit overuse of a specific resource. This could be an API rate limit, slow network connection, slow disk I/O operation or limited CPU/RAM. Starting a huge number of go routines that use a limited resource could cause your application to crash. If you are experiencing crashes or errors on a resource intensive task then I would recommend implementing a concurrency limiter....