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

 GO


  GoLang to build smaller executable file

Normally the executable file built with go is a bit large, it is always desired that a smaller executable file should be generated though. In this post, a couple of ways to reduce the size of the executable will be introduced. The end effect is that the executable file size would be much less than the normal generated one.The file which is built normally has below size.Mode LastWriteTime Length Name---- ------------- ------ -----a---- 12/14/2019 9:47 AM 1974272 main-ori.exeAdd build flags Two ld parameters can be added when usin...

10,622 2       EXECUTABLE GOLANG


  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,077 0       GOLANG GVM GVM PKGSET RVM


  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,373 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,786 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,856 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,638 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,509 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,435 1       NIL VALUE NIL TYPE NIL CHECK INTERFACE GOLANG