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