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

 GO


  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,545 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,480 1       NIL VALUE NIL TYPE NIL CHECK INTERFACE GOLANG


  Can two new objects point to the same memory address in GoLang?

Do you have any idea what the output will be for below GoLang snippet?package mainimport ( "fmt")type obj struct{}func main() { a := &obj{} fmt.Printf("%p\n", a) c := &obj{} fmt.Printf("%p\n", c) fmt.Println(a == c)}Many people would think that a and c are two different object instances which have different memory addresses. Hence a == c will be false. But if you try to run the above program, you would see below output0x5781c80x5781c8trueTo get to know the reason why the comparison returns true, some understanding of how Go allocates memory and how variable memory escape happens are ne...

12,301 0       ZEROBASE VARIABLE ESCAPE GOLANG GO


  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,331 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,880 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,063 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,389 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,798 2       PROGRAMMING GOLANG