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

 GO


  Understand unsafe in GoLang

Before going to understand unsafe package in GoLang, the first thing needs to talk about is the pointer in GoLang. If you have a background of C language, you must know what pointer means and its usage. With pointer, you are free to operate any data at memory level which means you have great power, but this means that you have great responsibility as well. That's why it might be considered unsafe in lots of cases.Take a look at a simple example of doubling an integer.package mainimport "fmt"func double(x int) { x += x}func main() { var a = 3 double(a) fmt.Println(a) // 3}The above ...

17,541 0       ZERO-COPY UNSAFE GOLANG


  Be careful about nil check on interface in GoLang

nil check is frequently seen in GoLang code especially for error check since GoLang's special error handling convention. In most cases, nil check is straight forward, but in interface case, it's a bit different and special care needs to be taken.Take a look at below code snippet and guess what the output will be.package mainimport ( "bytes" "fmt" "io")func check(w io.Writer) { if w != nil { fmt.Println("w is not nil") } fmt.Printf("w is %+v\n", w)}func main() { var b *bytes.Buffer check(b) fmt.Printf("b is %+v", b)}The output will be:w is not nilw is b is In the check() method, you would expe...

16,483 1       NIL VALUE NIL TYPE NIL CHECK INTERFACE GOLANG


  Fix --go_out: protoc-gen-go: plugins are not supported

When generating RPC code using proto file template in GoLang, one may face the issue like below when running the command protoc.Error: exit status 1Output: --go_out: protoc-gen-go: plugins are not supported; use 'protoc --go-grpc_out=...' to generate gRPCSee https://grpc.io/docs/languages/go/quickstart/#regenerate-grpc-code for more information.Normally this issue is caused because the version of protoc-gen-go being used is not correct. One way to fix the issue is to follow the instructions provided by Google to run a new command.Another way to fix this is by having the Github version of proto...

16,011 0       PLUGIN GOLANG PROTO BUFFER


  Fix 'this authentication plugin is not supported' issue while using Go to connect MySQL 8

MySQL 8 has changed its default authentication plugin from mysql_native_password to caching_sha2_password to improve its security. However many third party libraries seem act slowly to catch up with this change. This causes some compatible issues with their connection to MySQL. One of the issues is seen in Go libraries while it's trying to connect to MySQL 8.The specific error has been observed is "this authentication plugin is not supported". The root cause of this issue is that the go-sql-driver didn't support the new default authentication plugin. This bug has been reported a coup...

14,715 4       GO MYSQL 8 AUTHENTICATION PLUGIN MYSQL


  Run JavaScript in GoLang

In some cases, there might be some JavaScript code needs to be ran in a GoLang project. Fortunately, there are a few libraries which support running JavaScript in GoLang. The most famous one would be v8. This is a Go API for the famous v8 JavaScript engine originally developed for the Chrominum project and the foundation of NodeJS.In this post, we will show some simple steps to get v8 working in a GoLang program on MacOS. First you need to install the package so that you can import it. Run belowgo get github.com/augustoroman/v8Unluckily you would encounter an error where some header file ...

14,545 0       JAVASCRIPT V8 GOLANG


  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,926 0       GOLANG GO 1.16 GO INSTALL


  The magic of go:linkname

When writing Go program, there is frequent need on using time.Sleep() function to pause the logic for some time. And if jumping to the definition of this function, can see below definition:// Sleep pauses the current goroutine for at least the duration d.// A negative or zero duration causes Sleep to return immediately.func Sleep(d Duration)I's strange that there is no function body defined here. What happened? The actual definition of the function body is residing at runtime/time.go indeed.// timeSleep puts the current goroutine to sleep for at least ns nanoseconds.//go:linkname tim...

12,564 0       GOLANG GO:LINKNAME TRICKS


  JSON unmarshal in GoLang

In almost all mainstream programming languages, there is either built-in or third-party library to support parse JSON data because it is so popular for organizing and transferring data among different services. In GoLang, the built in json package also provides functions for marshalling and unmarshalling JSON data. Marshalling GoLang struct to a JSON string is relatively simple, just need to call json.Marshal(), it's a bit complicated to unmarshal an arbitrary JSON string into a GoLang struct object though. This post will try to walk through some scenarios of unmarshalling JSON string to GoLan...

12,427 0       JSON UNMARSHAL GOLANG EMPTY INTERFACE