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

 GO


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


  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,833 0       GOLANG SAMPLE SECURITY DES DESEDE 3DES


  A strange behavior of printing struct with nested struct in GoLang

Normally when trying to print a struct , we would use %v to show all data of the struct. It will print the default format for each field in the struct.%v the value in a default format when printing structs, the plus flag (%+v) adds field namesBut recently we observed a strange behavior when printing a struct with nested struct which has a String() string implemented, the %v format prints an 'unexpected' output per our understanding.  Let's see the example snippet first.package mainimport ( "fmt")type Inner struct {}type A struct { Inner FieldA string}func (i Inner) String...

7,798 2       PROGRAMMING GOLANG


  Popular Golang JSON libraries evaluation

JSON (Javascript Object Notation), a prevailing data exchange format, is widely used in various platforms and languages. Golang, of course, will never miss the support for JSON. And with its own standard library, such as those interfaces like the REST API from the API Service in Kubernetes, it can easily process JSON.Although Go’s library works great, we can still seek those open-source JSON libs in Github to maximize our efficiency. Then the features, performance, applicability of these libs are what we should put into consideration.And here comes my “evaluation report.”JSON...

7,603 0       JSON LIB COMPARISON JSON LIB FASTJSON GO-JSON


  Understanding GoLang interface

If goroutine and channel are considered as the foundation for GoLang concurrency, interface would be the key for data types in GoLang. In practical Go programming, almost all data types are built/used around interfaces, interface is the core of GoLang data structures.Go is not a typical OOP language, it has no class and inheritance concept syntactically. But it doesn't mean that there cannot be polymorphism in GoLang. Because of interface, it achieves the same polymorphism effect as in C++, though syntactically different.Although Go doesn't have concept of class, it allows different data types...

7,459 1       INTERFACE 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,902 0       NCPU GOLANG CPU


  golangci-lint to enable comment check for exported functions

golangci-lint is a command line tool which aggregates a list of different go linters to check whether the source code is in correct condition from different aspects. It is built to run during the CI pipeline so that there is no obvious coding issues before compiling and building the program.It is easy to run it with just below command$ golangci-lint run -vINFO [config_reader] Config search paths: [./ /Users /] INFO [config_reader] Used config file .golangci.yml INFO [lintersdb] Active 10 linters: [deadcode errcheck gosimple govet ineffassign staticcheck structcheck typecheck unused varcheck] I...

6,698 1       GOLANG GOLANGCI-LINT REVIVE GOLINT EXPORTED COMMENT


  What is the use of empty struct in GoLang

In Go, an empty struct struct{} is a struct with no fields that may appear to be of little use, but in reality, it can be useful in certain situations and become a simple and efficient solution in code.As a semaphore or lockBecause the empty struct has no fields, it can be conveniently used to implement some concurrency control functions, such as mutex locks, read-write locks. We can use chan struct{} to implement an unbuffered channel for controlling concurrent access.package mainimport ( "fmt" "sync")func main() { var wg sync.WaitGroup var mu sync.Mutex c := make(chan struct{}...

6,695 2       GOLANG EMPTY STRUCT