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

Debugging like a Pro: Lessons from My Time at Spotify

#debugging #automation #softwareengineering Useful Logging Logs are one of the most basic and vital sources of information when debugging an application. Here are a few tips to make your logs more useful: Aggregation is a must. In a distributed system, logs must be aggregated into a single location so that they can be queried. Related logs should be linked by a unique, queryable identifier. Logs that originate from a single HTTP request should all have the same ID so that the flow of logic and data can be easily queried....

Golang Anti-Pattern: Why You Shouldn't Return Interfaces

Returning an interface in Golang will reduce type safety and increase complexity of your code. Instead of returning an interface, you should return a concrete type and pass it directly to a function that accepts an interface your concrete type implements.

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

Mastering Concurrency in Go Synchronizing Critical Sections With Mutex (Part 3)

Part 1 , Part 2 , Part 3 , Part 4 , Part 5 In Go, critical sections refer to parts of your code that manipulate a resource which must be accessed by only one goroutine at a time. By using a mutex, you can temporarily lock a critical section, perform the operation, and then unlock it, allowing other goroutines to access the resource. Attempting to lock a critical section in a goroutine will block execution until the lock can be acquired....

Preparing for a Live Coding Interview: Strategies for Success

While similar in some ways to software development, live coding interviews demand a unique approach. In everyday software development, you build knowledge and experience over time, getting more proficient each day. In a live coding interview, you’ll be expected to showcase your grasp of essential software development concepts, as well as data structures and algorithms, all within a time frame of roughly 45 minutes. Most data structures and algorithms are actually the first thing implemented in any language, so it’s extremely rare for a software developer to re implement a data structure in the day to day job....

Mastering Concurrency in Go: Fan in With Channels (Part 2)

Part 1 , Part 2 , Part 3 , Part 4 , Part 5 Return values from go routines cannot be captured in the usual way with the = operator because starting a go routine is non blocking. A channel can be used to collect the results from many concurrent go routines. Write a value to a channel inside the go routine that is doing the work, then in main() read from the channel until it is closed to collect all the results from the go routines....

Mastering Concurrency in Go: With Fanout and Goroutines (Part 1)

Part 1 , Part 2 , Part 3 , Part 4 , Part 5 Goroutines can be used to take advantage of CPUs with multiple processors. Starting a Goroutine using the “go” keyword, will start execution on a thread in parallel if a thread is available. You will need to make sure that your main function does not exit before all the goroutines finish their work. Playground: https://go.dev/play/p/FTJLzwY34l2 package main import ( "fmt" "sync" ) func main() { wg := sync....