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

 ALL


  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,248 2       STACKOVERFLOW GOLANG FMT


  Behavior of defer function in named return function

In Go, there is a special concept of named return value in function where a returned value can have name. For example, below is a named function in Go.func returnNamed() (i int) { i = 1 return}When the function returns, the return value i will have a value of 1. Also, Go has a concept of defer which will execute a function just before the calling function exits. This is similar to what finally block does in other languages such as Java. For example, a defer function can befunc deferFunc() { defer func() { fmt.Println("In defer") }() fmt.Println("Start")}Both return and defer can change th...

3,867 0       GOLANG DEFER NAMED RETURN DIFFERENCE


  Advantages and disadvantages of GoLang

GoLang is a strong typed language which means it is less flexible than interpreted languages by nature. But Go provides Any type(interface) and Reflect mechanism which make the language very close to interpreted languages on flexibility. More and more people start to learn GoLang.This post is mainly for listing down some of the advantages and disadvantages of GoLang.AdvantagesPerformance(Machine code)GoLang is a compilation language which can be compiled to machine code and the compiled binary can be directly deployed to target machine without extra dependency. The performance is better than t...

12,022 2       ADVANTAGE DISADVANTAGE GOLANG GOROUTINE GENERICS


  Use Delve to debug GoLang program

If you don't know how to debug program, you are not a real programmer.gdb can be used to debug go program, but according to golang website, delve is a better option. Note that Delve is a better alternative to GDB when debugging Go programs built with the standard toolchain. It understands the Go runtime, data structures, and expressions better than GDB.Below is a simple go program.package maintype Person struct { Name string Age int}func main() { var me Person me.Name = "Melvin" me.Age = 41 var wife Person wife.Name = "Raye" wife.Age = 36 var daughter Person daughter.Name = "Kat...

3,362 0       DEBUG GOLANG DELVE


  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,783 2       PROGRAMMING GOLANG


  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,518 0       JSON GOLANG EMPTY SLICE NIL SLICE