Today we’re going to talk about good and bad practices in Go. Many of these tips are just opinions, based on our experiences with Go applications. We use Go regularly and have built apps for mobile, web, desktop and servers. With the versatility provided by the Go language and LLVM compiler, not all tips, tools or best practices are applicable to these various environments. Thus we want to talk about some core conventions that are likely to be among the most valuable and simple to use.
Go is a simple language. You will get the best results in Go, by embracing the simplicity provided with the standard library. All of which is readily available online in easy to digest documentation.
Gophers Love Interfaces
If the standard library wasn’t the first indicator, take these words to heart: use interfaces everywhere.
Once upon a time, I hated interfaces. I saw them as redundant, overbearing, and unnecessary. While some of those arguments may have still hold true in other languages (particularly scripted ones) Go really shines with rigidity. This is true, because YOU will improve as you embrace the Go way.
The rigidity of interfaces help you to architect better making for cleaner code and better tests. Interfaces also help you conform to standards that will allow Go do what it does best: concurrency. Generally speaking, it is far better to have too many interfaces than too few of them.
Use “gofmt” to Go Format Code
For whatever reason, gofmt is often overlooked or unused by solo or new Golang developers. Gofmt can usually be tied in to your favorite IDE or editor so that it is called on save of a .go file; giving you the consistency of default Go formatting without the hassle. In addition to using gofmt, you should check out golint and go vet.
Dependency Management in Golang
Using Godeps for specific versions is more ideal than just relying on the import directive. Godeps uses a simple bash script that uses a custom file called Godeps which lists the packages to install. This works great for deploys to heroku or to include in your repo to share with a team.
We highly recommend reading and thank Peter for his tips and blog post on Go best practices. Hope you enjoyed the podcast, and we look forward to your comments or suggestions @GoLangCastTV on twitter.