Today's Question:  What does your personal desk look like?        GIVE A SHOUT

 GO


  New function signal.NotifyContext in GoLang 1.16

os/signal package in GoLang may not be frequently used but it provides some good features like Shutdown() which can be used to gracefully shutdown a running HTTP server.func (srv *Server) Shutdown(ctx context.Context) errorWith this function, there is no need to use third party library to gracefully shutdown HTTP server. How is it being used?package mainimport ( "context" "fmt" "net/http" "os" "os/signal" "time")func main() { server := http.Server{ Addr: ":8080", } http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { time.Sleep(time.Second * 10) fmt.Fprint(w, "Hello world!") ...

6,532 0       GRACEFUL SHUTDOWN NOTIFYCONTEXT GOLANG


  Restore mocked variables in GoLang unit test

One of the guarding principles of writing unit test is that there should be no real external calls for dependant services. Unit test should run by its own and can run without issues on any environment including local, build, test environment. This indicates there should be some mock responses whenever an external call is needed so that different unit test scenarios can be covered.How can this be done in GoLang? In GoLang, anything can be assigned to a variable even including functions. A variable can be created to reference a function and it can be called just like a normal function when neede...

6,381 0       UNIT TEST GOLANG MOCK FUNCTION RESTORE MOCK


  Demo on creating worker pool in GoLang

A worker pool is a pool where a specified number of workers(usually goroutine) created and run to pick up tasks. This can allow multiple tasks to be ran at the same time while keeping the number of workers a fixed number to avoid overuse of resource in the program.There are usually two approaches of creating worker pool.One is with fixed number of workers pre-createdOne is creating worker when needed until the max number of workers createdIn this post, we will cover the demonstration of creating fixed number of workers pre-ahead. This approach is usually used when one knows there are lots of t...

6,336 0       WORKER POOL GOROUTINE GOLANG


  Will nil == nil be true in GoLang

There is some interview question may ask whether nil == nil be true in GoLang. Wil it be true. false or compilation error? To know the answer, some knowledge about nil in GoLang needs to be explained first.nil definitionAccording to Go's official documentation, the definition of nil is// nil is a predeclared identifier representing the zero value for a// pointer, channel, func, interface, map, or slice type.var nil Type // Type must be a pointer, channel, func, interface, map, or slice typenil is the zero value of types like pointer, channel, func, interface, map or slice. Itself is NOT a GoLa...

5,867 2       GOLANG NIL NIL EXPLANATION


  Install multiple versions of Go

As a developer, there might be need in some cases to have multiple versions of Go installed. One may test out some new features of the latest version of Go but also wanna maintain a stable version of Go for daily development purpose. How to maintain multiple versions of Go?The GoLang official website has provided a way to maintain multiple versions of Go by using go get to install the different versions of Go.Below are steps:Download a normal version of Go and install itGo to the Go release page to find out which version of go to installRun the go get command to download the version and instal...

5,250 0       GO VERSION GO BINARY MULTIPLE VERSION


  Time to think about supporting max/min functions for integers in GoLang

Sometime back we wrote a post explaining why there is no max/min function support for integers in GoLang, one of the reasons is that no overloading is supported and the function name has been used for other numeric types float64. To solve this problem and make max/min function support integer as well, either GoLang should support overloading like in Java or the same set of functions need to be created in a different package other than standard math. These don't look like good options as overloading is more a OOP concept and supporting the same set of functions in a different package doesn't so...

4,928 0       GOLANG GENERICS MAX MIN


  errGroup in GoLang explained

Today, let's talk about the usage and applicable scenarios of errGroup, which is often used. Why is it useful? Generally, when we use goroutine, we cannot return a value. If you want to pass out the result of the goroutine execution, you usually have to use the channel. The errGroup package is suitable if you want to know when the goroutine you opened encounters an error during execution and stop working, and I need to know the error value.errGroup usageThe package needs to be downloaded and installed first. go get -u golang.org/x/syncAn example of using it would bepackage mainimport ( ...

4,885 0       CONCURRENCY GOLANG ERRGROUP WAITHROUP


  A simple tutorial on GoLang connecting to Clickhouse

Go, also known as Golang, is a statically-typed, concurrent programming language created by Google. ClickHouse is a high-performance, column-oriented database management system that can be used for real-time data analysis.This tutorial will provide a deep dive into how to connect to ClickHouse from a Go program, including how to perform common database operations such as SELECT and INSERT statements.Before proceeding, it is assumed that you already have Go and ClickHouse installed on your machine. If not, you can download the latest version of Go from the official website and install it by fol...

4,542 0       TUTORIAL GOLANG CLICKHOUSE