From 3bcc9f5581dce854ac4105d01ccca93b9d76a15e Mon Sep 17 00:00:00 2001 From: Dan Markham Date: Sat, 3 Oct 2026 00:04:15 -0700 Subject: [PATCH] Move gomods example to the go tool directive The example still used go 1.12 and told readers to go get -u enumer, generate, then go mod tidy to drop it again. With Go 1.25 required anyway, pin enumer with a tool directive so go generate works on a fresh clone and tidy keeps the dependency. - examples/gomods/go.mod: go 1.25, tool github.com/dmarkham/enumer - examples/gomods/main.go: go:generate go tool enumer - examples/gomods/README.md: new steps - README.md: state the Go 1.25 floor and lead the install section with the tool directive, pointing at the example - README.md: remove the retired Go Report Card badge --- README.md | 31 ++++++++++++++++++------------- examples/gomods/README.md | 13 +++++++++---- examples/gomods/go.mod | 12 +++++++++++- examples/gomods/go.sum | 12 ++++++++++++ examples/gomods/main.go | 2 +- 5 files changed, 51 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 459f9b6..701205c 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,6 @@ # Enumer [![GoDoc](https://godoc.org/github.com/dmarkham/enumer?status.svg)](https://pkg.go.dev/github.com/dmarkham/enumer) -[![Go Report Card](https://goreportcard.com/badge/github.com/dmarkham/enumer)](https://goreportcard.com/report/github.com/dmarkham/enumer) [![GitHub Release](https://img.shields.io/github/release/dmarkham/enumer.svg)](https://github.com/dmarkham/enumer/releases) Enumer generates Go code that adds useful methods to enums (constants with a specific type). @@ -37,26 +36,34 @@ It generates the same `String()` method plus the extras below, so existing code ## Install -Pick one: +Enumer needs Go 1.25 or newer. + +The recommended setup pins enumer in `go.mod` as a tool dependency, so `go generate` works on a +fresh clone with no separate install: ```sh -# Run it on demand from go:generate. No install needed. -//go:generate go run github.com/dmarkham/enumer@latest -type=Pill +go get -tool github.com/dmarkham/enumer@latest +``` -# Or install a binary on your PATH. -go install github.com/dmarkham/enumer@latest +```go +//go:generate go tool enumer -type=Pill ``` -Prebuilt binaries for Linux, macOS, and Windows are on the -[releases page](https://github.com/dmarkham/enumer/releases). +[examples/gomods](examples/gomods) is a runnable module set up this way. -If you prefer to pin the version in `go.mod`, add it as a tool dependency and `go mod tidy`: +Two other options: ```sh -go get -tool github.com/dmarkham/enumer@latest -//go:generate go tool enumer -type=Pill +# Run on demand from go:generate without touching go.mod. +//go:generate go run github.com/dmarkham/enumer@latest -type=Pill + +# Install a binary on your PATH. +go install github.com/dmarkham/enumer@latest ``` +Prebuilt binaries for Linux, macOS, and Windows are on the +[releases page](https://github.com/dmarkham/enumer/releases). + ## Usage ``` @@ -193,8 +200,6 @@ Several types at once with a custom file name: enumer -type=Pill,Day -output=enums_gen.go ``` -A runnable module is in [examples/gomods](examples/gomods). - ## History Enumer started as a fork of Rob Pike's stringer, was extended by diff --git a/examples/gomods/README.md b/examples/gomods/README.md index 8d96aa0..0fbbaeb 100644 --- a/examples/gomods/README.md +++ b/examples/gomods/README.md @@ -1,8 +1,13 @@ # Go Modules Sample +Shows how to run enumer from `go generate` with the tool pinned in `go.mod`. + ## Steps - 1. Go get enumer `go get -u github.com/dmarkham/enumer` - 2. `go generate` This should create `pill_enumer.go` - 3. `go run *.go` to see it in action - 4. `go mod tidy` to remove the deps for `enumer` once your happy. +1. `go get -tool github.com/dmarkham/enumer@latest` adds a `tool` directive to `go.mod`. + This module already has one, so you can skip this step. +2. `go generate` creates `pill_enumer.go`. +3. `go run .` prints the enum values and a couple of method results. + +The `tool` directive keeps the dependency through `go mod tidy`, so `go generate` +works on a fresh clone without any extra install. It needs Go 1.25 or newer. diff --git a/examples/gomods/go.mod b/examples/gomods/go.mod index ae7bd39..a71e28d 100644 --- a/examples/gomods/go.mod +++ b/examples/gomods/go.mod @@ -1,3 +1,13 @@ module github.com/dmarkham/gomods -go 1.12 +go 1.25.0 + +tool github.com/dmarkham/enumer + +require ( + github.com/dmarkham/enumer v1.6.4 // indirect + github.com/pascaldekloe/name v1.0.0 // indirect + golang.org/x/mod v0.37.0 // indirect + golang.org/x/sync v0.21.0 // indirect + golang.org/x/tools v0.47.0 // indirect +) diff --git a/examples/gomods/go.sum b/examples/gomods/go.sum index e69de29..c3ffbc0 100644 --- a/examples/gomods/go.sum +++ b/examples/gomods/go.sum @@ -0,0 +1,12 @@ +github.com/dmarkham/enumer v1.6.4 h1:52p5gJBf9COja834f5/QsK4HjTTzRODi823Vu84slIQ= +github.com/dmarkham/enumer v1.6.4/go.mod h1:eMZE0SngYz1tyRVe09xKxf5R1RE9uJAFO4XckaCiTfM= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/pascaldekloe/name v1.0.0 h1:n7LKFgHixETzxpRv2R77YgPUFo85QHGZKrdaYm7eY5U= +github.com/pascaldekloe/name v1.0.0/go.mod h1:Z//MfYJnH4jVpQ9wkclwu2I2MkHmXTlT9wR5UZScttM= +golang.org/x/mod v0.37.0 h1:vF1DjpVEshcIqoEaauuHebaLk1O1forxjxBaVn884JQ= +golang.org/x/mod v0.37.0/go.mod h1:m8S8VeM9r4dzDwjrKO0a1sZP3YjeMamRRlD+fmR2Q/0= +golang.org/x/sync v0.21.0 h1:HLII4xRRTtCRkxYp4HNFF0Js/Og6q2i++KXbg0gHCwM= +golang.org/x/sync v0.21.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0= +golang.org/x/tools v0.47.0 h1:7Kn5x/d1svx/PzryTsqeoZN4TZwqeH5pGWjefhLi/1Q= +golang.org/x/tools v0.47.0/go.mod h1:dFHnyTvFWY212G+h7ZY4Vsp/K3U4/7W9TyVaAul8uCA= diff --git a/examples/gomods/main.go b/examples/gomods/main.go index 7d38027..a8b768a 100644 --- a/examples/gomods/main.go +++ b/examples/gomods/main.go @@ -2,7 +2,7 @@ package main import "fmt" -//go:generate go run github.com/dmarkham/enumer -type=Pill -json +//go:generate go tool enumer -type=Pill -json type Pill int const (