errcode

command module
v0.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 19, 2023 License: BSD-3-Clause Imports: 14 Imported by: 0

README

Errcode is an error generation tool with error codes

The source code is based on https://github.com/golang/tools/tree/master/cmd/stringer, and on this basis, the function of obtaining the error code according to error is added.

Installation

go install github.com/fengxuway/errcode@latest

Usage

Here is help documention

$ errcode  -h                                                         
Usage of errcode:
        errcode [flags] -type T [directory]
        errcode [flags] -type T files... # Must be a single package
For more information, see:
        https://github.com/fengxuway/errcode
Flags:
  -codefunc string
        error code function name (default "Code")
  -linecomment
        use line comment text as printed text when present
  -output string
        output file name; default srcdir/<type>_errcode.go
  -tags string
        comma-separated list of build tags to apply
  -trimprefix prefix
        trim the prefix from the generated constant names
  -type string
        comma-separated list of type names; must be set
  -unknowncode int
        set unknown error code when not match (default -1)

In example/example.go source file, define Err and Err2 enum for error code, we can command:

errcode -type=Err,Err2 --linecomment -unknowncode=-1 -codefunc=Code

Then err_errcode.go source file is generated!

PS: generated file name always with _errcode.go as suffix.

Documentation

Overview

Stringer is a tool to automate the creation of methods that satisfy the fmt.Stringer interface. Given the name of a (signed or unsigned) integer type T that has constants defined, stringer will create a new self-contained Go source file implementing

func (t T) String() string

The file is created in the same package and directory as the package that defines T. It has helpful defaults designed for use with go generate.

Stringer works best with constants that are consecutive values such as created using iota, but creates good code regardless. In the future it might also provide custom support for constant sets that are bit patterns.

For example, given this snippet,

package painkiller

type Pill int

const (
	Placebo Pill = iota
	Aspirin
	Ibuprofen
	Paracetamol
	Acetaminophen = Paracetamol
)

running this command

stringer -type=Pill

in the same directory will create the file pill_string.go, in package painkiller, containing a definition of

func (Pill) String() string

That method will translate the value of a Pill constant to the string representation of the respective constant name, so that the call fmt.Print(painkiller.Aspirin) will print the string "Aspirin".

Typically this process would be run using go generate, like this:

//go:generate stringer -type=Pill

If multiple constants have the same value, the lexically first matching name will be used (in the example, Acetaminophen will print as "Paracetamol").

With no arguments, it processes the package in the current directory. Otherwise, the arguments must name a single directory holding a Go package or a set of Go source files that represent a single Go package.

The -type flag accepts a comma-separated list of types so a single run can generate methods for multiple types. The default output file is t_string.go, where t is the lower-cased name of the first type listed. It can be overridden with the -output flag.

The -linecomment flag tells stringer to generate the text of any line comment, trimmed of leading spaces, instead of the constant name. For instance, if the constants above had a Pill prefix, one could write

PillAspirin // Aspirin

to suppress it in the output.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL