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

 GO


  Go 1.16 is released

Note: The post is authorized by original author to republish on our site. Original author is Stefanie Lai who is currently a Spotify engineer and lives in Stockholm, original post is published here.Last week, Go1.16 was released, bringing relatively more changes than version 1.15, which was influenced by the epidemic.The update is in many aspects, including compilation, deployment, standard library, etc. In the official Go document, all changes are classified based on Tools, Runtime, Complier, etc.It is undoubtedly good to dig into all the changes, but sometimes we understa...

905 0       GOLANG GO1.16 NEW FEATURES


  How to check when an API is introduced in GoLang

Normally people would not pay much attention to which GoLang version is being used as lots of functions are backward compatible. However there are cases where GoLang version does matter as some functions may not be supported by old version of GoLang. For example, strings.Builder is introduced in Go 1.10, but below code would fail to be compiled on Go 1.10.package mainimport ( "fmt" "strings")func main() { var b strings.Builder b.WriteString("polarisxu") fmt.Println(b.Cap())}The compilation error is:$ go versiongo version go1.10.8 darwin/amd64$ go run main.go# command-line-arguments./main.g...

1,991 0       GO TOOL API VERSION GOLANG


  How to do pprof for gRPC service

gRPC is a RPC framework based on HTTP and is frequently used for communications among micro service inside the same organization network. However,  the service functions cannot be accessed via normal HTTP URL as it's not a WEB framework. In this case, how to do pprof on a gRPC service?The trick is starting a HTTP server asynchronously while starting the gRPC service. This HTTP server can be accessed to run prrof debug.go func(){ http.ListenAndServe(":10001", nil)}()Since it uses the default ServerMux, the pprof related routes will be registered automatically after importing the package&nb...

3,847 0       PPROF GOLANG GRPC


  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,307 0       GOLANG GOROUTINE WORKER POOL


  How does GoLang know how many CPUs to use?

When running lscpu command on Linux, it will list the CPU info on the machine. Take one example where there is one CPU with 2 cores and each core has two threads which indicates there are 4 cores available.Now let's see how many cores GoLang program would identify.From output, NumCPU and GOMAXPROCS both output 4 which is expected. How does go runtime get this info, does it get it through similar command like lscpu or /proc/cpuinfo? Let's dig more in GoLang's source code.In runtime/debug.go, it tells that this value is from the global variable ncpu which is set when process starts and...

6,860 0       CPU GOLANG NCPU


  About go get and go install in Go 1.16

Go version 1.16 beta1 has been released on 18 Dec 2020, major features of Go 1.16 have been finalized with this beta release. Many people are discussing about the support of Apple M1, however, this post will not cover this topic. Instead the focus will be on go get and go install changes.There are lots of changes related to modules in Go 1.16, the details can be found in the release note. Below are some of the key highlights.GO111MODULE is on by default, if wanna keep old behavior, needs to set it to auto. This means that GOPATH would gradually get away from us.go install can take a versi...

12,876 0       GOLANG GO 1.16 GO INSTALL


  The hidden risk of passing slice as function parameter

In Go's source code or other open source libraries, there are lots of cases where a slice pointer is passed to function instead of slice itself. This brings up a doubt why not passing slice directly as its internal is backed by an array pointer to point to underlying data?For example, in log package, the formatHeader function takes a parameter buf as type *[]byte instead of []byte.func (l *Logger) formatHeader(buf *[]byte, t time.Time, file string, line int) {}Let's understand the rationale behind this starting with an example.func modifySlice(innerSlice []string) { innerSlice[0] ...

7,965 2       GOLANG SLICE SLICE POINTER


  Mock Solutions for GoLang Unit Test

In Go development, Unit Test is inevitable. And it is essential to use Mock when writing Unit Tests.Mock can help test isolate the business logic it depends on, enabling it to compile, link, and run independently.Mock needs Stub. Stub function replaces the real business logic function, returns the required result, and assists the test.I involved the related test code for Controllers while writing Kubernetes Operator recently, and there would be mocks for GRPC and HTTP requests. I did it in an old fashion way, but I believe there is a better and more graceful way to han...

12,192 0       UNIT TEST GOMOCK GOSTUB TESTIFY