How to list installed go packages

goinstall is now history goinstall was replaced by go get. go get is used to manage external / 3rd party libraries (e.g. to download them, update them, install them etc). Type go help get to see command line help, or check out these pages: Command go About the go command (blog post) If you want … Read more

Why are interfaces needed in Golang?

Interfaces are too big of a topic to give an all-depth answer here, but some things to make their use clear. Interfaces are a tool. Whether you use them or not is up to you, but they can make code clearer, shorter, more readable, and they can provide a nice API between packages, or clients … Read more

How to reduce compiled file size?

If you are using a Unix-based system (e.g. Linux or Mac OSX) you could try removing the debugging information included in the executable by building it with the -w flag: go build -ldflags “-w” prog.go The file sizes are reduced dramatically. For more details visit the GDB’s page: http://golang.org/doc/gdb

How to set default values in Go structs

One possible idea is to write separate constructor function //Something is the structure we work with type Something struct { Text string DefaultText string } // NewSomething create new instance of Something func NewSomething(text string) Something { something := Something{} something.Text = text something.DefaultText = “default text” return something }

Example for sync.WaitGroup correct?

Yes, this example is correct. It is important that the wg.Add() happens before the go statement to prevent race conditions. The following would also be correct: func main() { var wg sync.WaitGroup wg.Add(1) go dosomething(200, &wg) wg.Add(1) go dosomething(400, &wg) wg.Add(1) go dosomething(150, &wg) wg.Add(1) go dosomething(600, &wg) wg.Wait() fmt.Println(“Done”) } However, it is rather … Read more