Notes on Go Development

Page content

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 Homebrew. 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.

If you use Visual Studio Code, install the Microsoft plugin for Go. Use the vim-go plugin for Vim. 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, Visual Studio Code will provide you with access to information and features from Git directly in their user interfaces.

Code Quality

Use golangci-lint to run a suite of quality checks on your code. This tools works with popular code editors and IDEs. You should also add 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 and above include 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

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 one of the frameworks. For example, 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 Serverless or Sparta.

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 option 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 bbolt instead of a separate database. The bbolt package implements a file-based key-value database system in pure Go.

Developing Command-line Tools

The Go standard library includes the basic elements that you need to build your own command-line tools. Use the Cobra framework to create command-line tools with features such as sub-commands and autocompletion.

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
  • OpenAPI Toolkit - Go libraries for OpenAPI
  • Plush - Powerful templating system
  • Viper - Configuration for Go applications

Resources

This article lists useful learning resources for Go.