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

 GO


  Some tricks and tips for using for range in GoLang

GoLang provides two major ways to loop through elements of array, slice and map. They are for and for range. Many people find that for range is very convenient when don't care about the index of the element. In this post, some tricks and tips would be talked about regarding for range.1. Loop and get pointer of each elementAssume there is a code snippet like below which is to get the pointer of each element in an array and create a new array with the corresponding pointer.arr := [2]int{1, 2}res := []*int{}for _, v := range arr { res = append(res, &v)}//expect: 1 2fmt.Println(*res[0],*res...

29,188 0       FOR RANGE GOLANG FOR LOOP POINTER


  A mini post on GoLang context

In a GoLang web server, every request coming in will be handled by a goroutine. In the request handler, the logic may also need to create new goroutine to handle other tasks like RPC call. When the request is processed and response is returned, these goroutines created need to be exited so that no goroutine leak should happen.package mainimport ( "fmt" "log" "net/http")func main() { http.HandleFunc("/echo", func(w http.ResponseWriter, r *http.Request) { fmt.Println(&r) w.Write([]byte("hello")) }) log.Fatal(http.ListenAndServe(":8080", nil))}go run main.go0xc0000060100xc0000760480xc000076...

11,849 0       GOLANG CONTEXT


  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,676 2       GOLANG EXECUTABLE


  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,125 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,429 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,836 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,882 0       GOLANG SLICE


  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,810 12       GOLANG INT64 MAX INT