Notes on Go Development

Posted in programming with tags golang -

Summary

Notes on developing software with the Go language.

Installing Go

To install Go on Windows, download it from the official Website. Choose the 64-bit Windows installer package for the current version, unless you know that you need a different option.

To install Go on macOS or Linux, use gimme. The gimme tool enables you to choose which versions of Go to install on your system, and switch between them. Homebrew on macOS also provides current versions of Go. Linux distributions supply versions of Go, but these will frequently be older than the current release.

Docker, Inc. maintain Docker images for Go. These are useful for Continuous Integration, where you need containers that include a Go compiler to build releases of your software.

Essential Tools

The Go Plugin for Your Code Editor

The Open Source community around Go provides a range of tools for code quality and refactoring that are designed to be integrated with both editors and automated build processes. Every popular editor has a plugin for Go support, which can use these standard utilities to check and refactor your code as you work.

I currently use Neovim, with the vim-go plugin. If you do not have a favourite text editor, Visual Studio Code is a high-quality and customisable editor that has a well-maintained plugin for Go. Both of these editors are provided free of charge. If you would prefer to use an IDE, JetBrains offer GoLand as a commercial product.

Git for Version Control

If you do not already use version control, you should install Git on your system. Git is now effectively the standard version control tool for developers.

Version control is obviously vital for collaborating with other programmers. It also enables you to efficiently copy your application to other systems for testing, deployment and backup.

If you have installed Git, Atom and Visual Studio Code will provide you with access to information and features from Git directly in their user interfaces. If you use Visual Studio Code, you should also consider adding the Git Lens extension, which enhances the integration with Git.

Code Quality

Use either Go Meta Linter or golangci-lint to run a suite of quality checks on your code, including the official linter. The Go plugins for text editors and IDEs often support the Go Meta Linter, and may support golangci-lint. For example, the Go plugin for Visual Studio Code installs the official linter and Go Meta Linter, but you must enable quality check suites. You should also add Go Meta Linter or golangci-lint to your Continuous Integration process, to ensure that the code that is submitted passes quality checks.

The Go Report Card service analyses the Go software in public Git repositories, using some of the standard quality checks. You do not need to do anything to enable the service to analyse your project. If you wish, you may add a badge to the README in your repository that links to the Report Card for your project.

Dependency Management

Version 1.11 of Go includes initial support for modules. The previous standard for managing dependencies was dep.

Other Useful Tools

  • Delve - Debugger
  • goimports - Removes unused import statements from Go code
  • Gomacro - Interactive interpreter for Go
  • GoReleaser - Release automation for Go projects
  • Packr - Embeds files into Go binaries

Code editors with a Go plugin will integrate with Delve and goimports.

Error Handling

Go assumes that error objects should be designed to match the needs of the particular application. The errors package provides error objects that include stack traces.

Web Applications

You can develop Web applications in Go with just the standard library and a few third-party packages, but you will need to handle many features and technical decisions yourself. These frameworks provide conventions and sets of tested components for your applications:

  • chi - A minimal router and middleware framework for Web API services.
  • Buffalo - A full set of integrated tools and components for Web sites and applications.
  • Echo - A small, convenient framework for Web applications
  • Go kit - Microservices in enterprise architectures

The Gorilla toolkit is a popular collection of packages for developers who prefer to assemble their applications from individual components, rather than use a framework.

Many developers find that the router that is included in the standard library for Go is not flexible enough to support their applications, and decide to use other routers. The mux package from the Gorilla toolkit is often used as a direct replacement for the router that is provided by the Go standard library.

Go Web applications can be deployed on a very wide variety of infrastructure. Cloud services such as Google App Engine, Heroku and Red Hat OpenShift provide low-maintenance hosting, but you can set up any server to run Go applications.

To produce applications for AWS Lambda, use the Serverless, Sparta or Apex frameworks.

Web Clients

The standard library for Go includes HTTP server and client software. These support both the HTTP 1.1 and HTTP/2 protocols. Use resty if you would like a more convenient API for REST client software.

The goquery library provides an implementation of the jQuery API for HTML parsing. Colly is a complete framework for Web scraping.

Accessing Databases

Go includes support for SQL in the standard library. This package provides the basic components that are needed for database access, and you can use it in your own applications, but it is most valuable for the authors of libraries. In most cases, you should use a third-party database library.

  • sqlx - A convenient data access API that follows the conventions of the Go standard library
  • sqlboiler - A tool that generates full sets of Go data access code for existing databases
  • GORM - The most popular Object Relational Mapper for Go
  • pop - An Object Relational Mapper that is inspired by ActiveRecord

Whichever package you choose, you will also need a driver for the specific brand of database that your code will access. The most popular drivers are:

If you only need to store sets of data on the computer that runs your application, consider using the bbolt package, which implements a file-based key-value database system in pure Go.

Developing Command-line Tools

To create command-line tools with Go, use the Cobra framework.

Robotics and Internet of Things

Gobot is the main package for working with robotics and hardware, such as Arduino boards.

Other Useful Libraries

  • Blackfriday - Markdown processor
  • decimal - Decimal support for Go
  • etree - Parses and generates XML
  • Fake - Generates fake data
  • go-cmp - Package for comparing Go values in tests
  • gonum - Packages for numeric and scientific work
  • logrus - Structured logging for Go applications
  • Plush - Powerful templating system
  • Viper - Configuration for Go applications

Resources

Community

References

  • Awesome Go - List of online resources for Go
  • Go 101 - Online book for learning Go
  • Go by Example - Library of code examples
  • GoDoc - Online copies of documentation for public Go projects
  • Go Report Card - Quality reports for public Go projects
  • GoSearch - Search engine for Go packages

Best Practices

Tutorials

  • Golangbot - Beginner tutorials on aspects of Go
  • Gophercises - Programming exercises for learning Go

Books

Videos

Next article
Backups with restic