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

 GO


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


  Empty slice vs nil slice in GoLang

In Go, there is a type called slice which is built on top of array. It's a very convenient type when we want to handle a group of data. This post will explain a subtle but tricky difference between empty slice and nil slice.A nil slice is a slice has a length and capacity of zero and has no underlying array. The zero value of slice is nil. If a slice is declared like below, it is a nil slice.package mainimport "fmt"func main() { var a []string fmt.Println(a == nil)}The output will be true for above code snippet. An empty slice is a slice also has a length and capacity of zero but has unde...

33,517 0       JSON GOLANG EMPTY SLICE NIL SLICE


  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,137 0       POINTER FOR LOOP GOLANG FOR RANGE


  A simple example on implementing progress bar in GoLang

Sometimes when handling a long running process, there would be need to track the progress so that people know something is still running instead of doubting something goes wrong. In this case, a progress bar which indicates the current status and progress would be desired.This post will show an example on how to implement progress bar using GoLang. Let's take a look at the final outcome first before jumping into the implementation detail.The key in the implementation is actually just the \r control flag for fmt.Printf(). \r is actually the carriage return control which tells the cursor to move...

21,296 5       TUTORIAL EXAMPLE GOLANG PROGRESS BAR


  Be careful about printing error as string in GoLang

In GoLang, we can format and produce string using fmt.Printf(), just like C, GoLang also supports format verbs like %s, %d which can be placeholder for different types of values. But please pay attention when printing error as string so that you will not fall into some trap.Let's first take an example code snippet and see what trap we are talking about.package mainimport "fmt"type A stringfunc (a A) Error() string { return fmt.Sprintf("%s is an error", a)}func main() { a := A("hello") fmt.Printf("error is %s", a)}What do you expect the output is? Do you expect "error is hello is an error"? Unf...

21,244 2       STACKOVERFLOW GOLANG FMT


  Run code with multiple files in the same main package in GoLang

To run a GoLang program, there needs to be a main() function defined. In some cases when developing some demo program which has multiple files and just wanna put them in the same main package and this folder is not in GOPATH, how to run the program?Let's say we have following folder structure where the main() function is defined in main.go.If you just run below command, it would fail to start to run the program and gives some error if some struct is defined in other files and being used.PS D:\Project\Go\sourcecode_updater\v2> go run main.go# command-line-arguments.\main.go:35:11: undefined:...

20,288 0       GOLANG EXECUTABLE MAIN PACKAGE MULTIPLE FILE


  Implement struct no copy in GoLang

There is some case where some of the struct in GoLang which is not meant to be copied. For example, sometimes a global configuration which should have only one version passed around the whole application and should not be copied and modified.In GoLang, there is no intuitive solution on preventing copying of struct. But there is still some way which can be leveraged to help prevent this while developing the code. The trick is to define some struct implementing sync.Locker interface and has this struct embedded in any struct not meant to be copied.// noCopy may be embedded into structs which mus...

19,392 0       GO VET NOCOPY NO COPY


  How to check whether a struct implements an interface in GoLang

Unlike other programming languages like Java, when implementing an interface in GoLang, there is no syntax like below to explicit tell the relationship:type I interface {}type A struct implements I {}When looking at the code or running the code in GoLang, it might lead to a case one doesn't know whether a struct implements interface before trying to use it as some interface.According to GoLang spec, as long as a struct implements all functions of an interface, it is considered as having implemented that interface and can be used wherever that interface is expected.To check whether a struct imp...

19,187 0       GOLANG INTERFACE IMPLEMENTATION CHECK