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....