Richard Bucker

Pro Tip from Go Authors

Posted at — Jan 11, 2015

Dave Cheney recently tweet this:#golang top tip: fmt and log packages know how to print errors, prefer fmt.Printf(“Oops: %v\n”, err) to fmt.Println(“Opps:”, err.Error())What makes this interesting is that I also saw something very similar in a live coding session. Andrew and Brad used “%v” all of their Printf functions. I do not have a concrete explanation, however, my intuition tells me that the Printf functions interpret the “%v” and then use some reflection and stringer -ification. This is because they seems to use it to display integers and strings. In a pragmatic way it makes sense to use the “%v” so that you do not have to modify the format string and the variables but you might have to do that anyway. At least you’d be able to change(refactor but not really) the types without having to chase down all of its usage.