Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -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).
Expand Down Expand Up @@ -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

```
Expand Down Expand Up @@ -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
Expand Down
13 changes: 9 additions & 4 deletions examples/gomods/README.md
Original file line number Diff line number Diff line change
@@ -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.
12 changes: 11 additions & 1 deletion examples/gomods/go.mod
Original file line number Diff line number Diff line change
@@ -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
)
12 changes: 12 additions & 0 deletions examples/gomods/go.sum
Original file line number Diff line number Diff line change
@@ -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=
2 changes: 1 addition & 1 deletion examples/gomods/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
Loading