Skip to content
Switch branches/tags
Go to file
1 contributor

Users who have contributed to this file

Tools as dependencies

Go modules support tools (commands) as dependencies. For example, you might need to install a tool to help with code generation, or to lint/vet your code. See the wiki for more details and background.

This example shows you how to add tools dependencies to your Go module, specifically the code generator stringer.

The resulting code can be found at


Create a module:

$ mkdir -p /home/gopher/scratchpad/tools
$ cd /home/gopher/scratchpad/tools
$ git init -q
$ git remote add origin
$ go mod init
go: creating new go.mod: module

Set GOBIN (see go help environment) to define where tool dependencies will be installed:

$ export GOBIN=$PWD/bin
$ export PATH=$GOBIN:$PATH

Add stringer as a dependency by importing the package in a build constraint-ignored file. This file will never be compiled (nor will it compile, because we are importing a main package); it is used simply to record the dependency. The file and the build constraint names are not particularly important, but we choose tools for the sake of consistency:

$ cat tools.go
// +build tools

package tools

import (
	_ ""

Install stringer:

$ go install
go: finding v0.0.0-20190511041617-99f201b6807e
go: downloading v0.0.0-20190511041617-99f201b6807e
go: extracting v0.0.0-20190511041617-99f201b6807e
go: finding v0.0.0-20190423024810-112230192c58
go: finding v0.0.0-20190311183353-d8887717615a
go: finding v0.0.0-20190308221718-c2843e01d9a2
go: finding v0.3.0
go: finding v0.0.0-20190215142949-d0b11bdaac8a

Our module reflects the dependency:

$ go list -m all v0.0.0-20190308221718-c2843e01d9a2 v0.0.0-20190311183353-d8887717615a v0.0.0-20190423024810-112230192c58 v0.0.0-20190215142949-d0b11bdaac8a v0.3.0 v0.0.0-20190511041617-99f201b6807e

Verify stringer is available on our PATH:

$ which stringer

Use stringer via a go:generate directive:

$ cat painkiller.go
package main

import "fmt"

//go:generate stringer -type=Pill

type Pill int

const (
	Placebo Pill = iota
	Acetaminophen = Paracetamol

func main() {
	fmt.Printf("For headaches, take %v\n", Ibuprofen)

go generate and run the result:

$ go generate
$ go run .
For headaches, take Ibuprofen

Commit and push the results:

$ cat <<EOD >.gitignore
$ git add -A
$ git commit -q -am 'Initial commit'
$ git push -q origin

Version details

go version go1.12.5 linux/amd64