Go plugin

The Go plugin builds Go modules, which are collections of packages stored in a file tree containing a go.mod file at the root. After a successful build, this plugin will install the generated binaries in $CRAFT_PART_INSTALL/bin.

Keywords

In addition to the common plugin and sources keywords, this plugin provides the following plugin-specific keywords:

go-buildtags

Type: list of strings Default: []

Build tags to use during the build. The default behavior is not to use any build tags.

go-generate

Type: list of strings Default: []

Parameters to pass to go generate before building. Each item on the list will be a separate go generate call. The default behavior is not to call go generate.

Environment variables

During build, this plugin sets GOBIN to ${CRAFT_PART_INSTALL}/bin.

Dependencies

The Go plugin needs the go executable to build Go programs but does not provision it by itself, to allow flexibility in the choice of compiler version.

Common means of providing go are:

  • The golang Ubuntu package, declared as a build-package.

  • The go snap, declared as a build-snap from the desired channel.

Another alternative is to define another part with the name go-deps, and declare that the part using the go plugin comes after the go-deps part. In this case, the plugin will assume that this new part will stage the go executable to be used in the build step. This can be useful, for example, in cases where a specific, unreleased version of go is desired but unavailable as a snap or an Ubuntu package.

How it works

During the build step the plugin performs the following actions:

  • If a go workspace has been setup by use of the go-use plugin, call go work use <build-dir> to add the source for the part to the workspace;

  • If not operating in the context of a go workspace, call go mod download all to find and download all necessary modules;

  • Call go generate <item> for each item in go-generate;

  • Call go install  ./..., passing the items in go-buildtags through the --tags parameter.

Examples

The following snippet declares a part using the go plugin. It uses the stable 1.22 version of the go snap, enables the build tag experimental and calls go generate ./cmd before building:

parts:
  go:
    plugin: go
    source: .
    build-snaps:
      - go/1.22/stable
    go-buildtags:
      - experimental
    go-generate:
      - ./cmd