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

 ALL


  Error handling in GoLang

Error handling is one of the must talked topics for any programming language. The program would be more reliable and stable if errors are handled properly and timely. Each programming language has its own way to handle error, this applies to GoLang as well. This post will discuss more about GoLang's error handling mechanism.Error handlingBefore talking more about GoLang's error handling, we can see how different programming languages are handling errors.C's error checkThe most direct way of checking error is using error code, and this is also how error is being checked traditionally. In proced...

3,362 0       GOLANG ERROR HANDLING FLUENT INTERFACE


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

913 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,993 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,882 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,315 0       WORKER POOL GOROUTINE GOLANG


  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,882 0       NCPU GOLANG CPU


  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,892 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,974 2       GOLANG SLICE SLICE POINTER