Henry Du Blog

think digital, act analog

Go Channel Patterns

Golang Channel Patterns Golang channel allows us to transfer data structure between go-routine boundaries. It could be zero byte data struct{}{}, served as signaling purpose. In this use case, the channel is semantically a signaling, by which, one go-routine can send a signal to another go-routine, as a notification or an event. Signaling without data serves the main purpose of cancellation. It allows one go-routine to signal another go-routine to cancel the channel.

Go Routine Run as a Thread?

Go Routine Run as a Thread? Whether or not a go-routine run as a machine thread is always a question for me, until I finally have a chance to inspect it by gdb. First, We could write a simple go code to have a go func(). package main import ( "fmt" "time" ) func main() { ch := make(chan string) go func() { fmt.Println(fmt.Sprintf("received the data '%v' from the channel", <-ch)) }() fmt.

Golang Pass Map Type Parameter

Golang Pass Map Type Parameter Observation When I read this blog section “But maps and channels are references, right?”, I realize that the following code should print true, because Golang pass the map by value, not map address. package main import "fmt" func fn(m map[int]int) { m = make(map[int]int) } func main() { var m map[int]int fn(m) fmt.Println(m == nil) } Analysis This is running result. When the program starts run main function, the active stack frame in memory belongs to main.

Local Postgres Container Setting Up

Local Postgres Container Setting Up This article has been done last couple of years, but it is still useful for developers who are during transition from old school services to the docker container world. For example, we want to implement a service to access local Postgres DB. We can actually apt-get install, yum install or brew install a postgres DB service. However, since we don’t want our local OS has heavy load to run Postgres all the time, using Docker to have a Postgres is a better choice.