Fiddling with different libraries and concepts in Golang
- Build Unix Like
wc
tool. ref - Build JSON parser ref
- Create a custom timeout handler. This is typically used to end any long-running request during API development.
Go allows us to call a function using go
and it will run as a separate goroutine. This is a very powerful primitive which allows us to easily parallelize parts of our code. However, we we run concurrent programs, we encounter problems that are common to this domain. These are:
func add(a, b int) {
return a + b
}
// invoke function as a go routine
go add(1, 2)
// create channel
ch1 := make(chan int) // sends and receives to an unbuffered channel is blocking
// create channel with buffer
ch2 := make(chan int, 100)
// send messages to a channel
ch1 <- 2
// receive messages from a channel
val := <-ch1
// close channel
v, ok := <-ch1 // ok is false when the channel is closed
// range on channel
for i := range ch1 // receives values from the repeatably until it is closed.
How to ensure equal distribution of messages among multiple goroutines created to do a specified job.
A common example of encountering this use case is when we have to create a worker pool that can listen to a set of jobs on a channel. This is used when executing DB queries in the application.
In Golang, if the channel is shared between multiple go-routines on the receiving side and when a barrage of messages are sent to the channel, those messages are equally distributed among all the go-routines on the receiving end. This is a very nice feature that is provided by default. ref
- context package. This is used to propagate timeline and deadline signals from one go-routine to another. A common example to use this is to implement a timeout for API requests. ref
- Timer and Ticker. This is used when we want to get a message once in the future or repeatedly. One real-world example of where it can be used is to implement rate-limiter