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

 ALL


  Maintain multiple versions of Go in one single environment

In a development environment, there might be multiple projects going on at the same time and they may require different development environments with different versions of build tool. In many programming languages, it is possible to have multiple versions of different build tool or development tool on a single environment. For example, there can be multiple JDKs, multiple versions of Ruby using RVM. For GoLang, there is a similar tool called GVM which can also be used to maintain multiple versions of Go in one single environment.In this post, we provide a mini guide on how to setup multiple ve...

8,087 0       RVM GVM PKGSET GVM GOLANG


  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,394 0       JSON UNMARSHAL GOLANG EMPTY INTERFACE


  Implementing DESede/ECB/NoPadding cipher algorithm in GoLang

By default, GoLang doesn't provide the ECB mode cipher for DESede though there is CBC mode provided. In cases we need to encrypt/decrypt data with ECB mode, we need to implement those by ourselves. This mode is frequently used when encrypting/decrypting PIN block which is small block data less than 16 bytes.In this post, we will introduce how to implement the DESede/ECB/NoPadding algorithm in GoLang by using the existing cipher support. Here we will not cover how DESede works in detail, instead we will just cover the logic to make it work.Below is the sample code which can be used to encrypt/d...

7,799 0       GOLANG SAMPLE SECURITY DES DESEDE 3DES


  The internals of slice in GoLang

There are 3 components of slice:a) Pointer: Points to the start position of slice in the underlying array;b) length (type is int): the number of the valid elements of the slice;b) capacity (type is int): the total number of slots of the slice.Check the following code:package mainimport ( "fmt" "unsafe")func main() { var s1 []int fmt.Println(unsafe.Sizeof(s1))}The result is 24 on my 64-bit system (The pointer and int both occupy 8 bytes).In the next example, I will use gdb to poke the internals of slice. The code is like this:package mainimport "fmt"func main() { s1 := make([]int, 3, 5) ...

3,866 0       SLICE GOLANG


  Why no max/min function for integer in GoLang

You may notice that there is no max/min function provided to compare the maximum/minimum of two or more integers if you are a GoLang developer with some experience . In other languages, these functions are provided as part of the core lib functions. Have you wondered why? Indeed GoLang provides max/min function in math package, but they are used for comparing float64 data type. The signature of these two functions aremath.Min(float64, float64) float64math.Max(float64, float64) float64The reason why GoLang provides the max/min function for comparing float64 numbers are that floating n...

55,705 12       GOLANG INT64 MAX INT


  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,523 0       V8 JAVASCRIPT 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,439 1       NIL VALUE NIL TYPE NIL CHECK INTERFACE GOLANG


  Can two new objects point to the same memory address in GoLang?

Do you have any idea what the output will be for below GoLang snippet?package mainimport ( "fmt")type obj struct{}func main() { a := &obj{} fmt.Printf("%p\n", a) c := &obj{} fmt.Printf("%p\n", c) fmt.Println(a == c)}Many people would think that a and c are two different object instances which have different memory addresses. Hence a == c will be false. But if you try to run the above program, you would see below output0x5781c80x5781c8trueTo get to know the reason why the comparison returns true, some understanding of how Go allocates memory and how variable memory escape happens are ne...

12,277 0       ZEROBASE VARIABLE ESCAPE GOLANG GO