stringer
介绍
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.
Stringer是一个自动创建满足fmt.Stringer接口的方法的工具。给定定义了常量(有符号或无符号)整数类型T的名称,stringer将创建一个新的自包含Go源文件实现。
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.
该文件与定义T的软件包在同一个软件包和目录中创建。它具有用于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.
Stringer最适用于连续值的常量,例如使用iota创建的常量,但无论如何都会创建良好的代码。将来,它还可能为位模式的常量集提供自定义支持。
安装stringer
stringer命令不是go安装包自带的,所以需要手动安装
# 下载命令
go get golang.org/x/tools/cmd/stringer
# 安装命令
go install golang.org/x/tools/cmd/stringer注意
stringer所在目录,需要添加到环境变量的path里面。
参数说明
| 参数 | 说明 |
|---|---|
| type | 自定义的类型名称 |
| output | 生成代码的文件名,默认名是{type}_string.go |
| linecomment | 使用常量所在行的备注,作为打印输出的信息 |
| trimprefix | 去掉常量名的前缀 |
type指定了需要generate的对象,后面加文件名的话,就是指定对象所在的文件名
stringer -type=Pill pill.go
使用案例
简单案例
创建一个Pill类型
package painkiller
type Pill int
const (
Placebo Pill = iota
Aspirin
Ibuprofen
Paracetamol
Acetaminophen = Paracetamol
)执行命令stringer -type=Pill
在同一目录中,将创建pill_string.go文件,内容如下:
import "strconv"
func _() {
// An "invalid array index" compiler error signifies that the constant values have changed.
// Re-run the stringer command to generate them again.
var x [1]struct{}
_ = x[Placebo-0]
_ = x[Aspirin-1]
_ = x[Ibuprofen-2]
_ = x[Paracetamol-3]
}
const _Pill_name = "PlaceboAspirinIbuprofenParacetamol"
var _Pill_index = [...]uint8{0, 7, 14, 23, 34}
func (i Pill) String() string {
if i < 0 || i >= Pill(len(_Pill_index)-1) {
return "Pill(" + strconv.FormatInt(int64(i), 10) + ")"
}
return _Pill_name[_Pill_index[i]:_Pill_index[i+1]]
}String()方法将Pill常量的值转换为相应常量名称的字符串表示,因此调用fmt.Print(painkiller.Aspirin)将打印字符串“Aspirin”。
通常,此过程将使用go generate运行,如下所示:
//go:generate stringer -type=Pill
使用注释
我们在创建常量时可以给常量加注释,使用stringer命令生成代码时可以用注释作为输出字符串。
创建Pill类型
package painkiller
//go:generate stringer -type=Pill -linecomment
type Pill int
const (
Placebo Pill = iota // 安慰剂
Aspirin // 阿司匹林
Ibuprofen // 布洛芬
Paracetamol // 扑热息痛
Acetaminophen = Paracetamol
)执行命令
//go:generate stringer -type=Pill -linecomment
生成代码如下:
import "strconv"
func _() {
// An "invalid array index" compiler error signifies that the constant values have changed.
// Re-run the stringer command to generate them again.
var x [1]struct{}
_ = x[Placebo-0]
_ = x[Aspirin-1]
_ = x[Ibuprofen-2]
_ = x[Paracetamol-3]
}
const _Pill_name = "安慰剂阿司匹林布洛芬扑热息痛"
var _Pill_index = [...]uint8{0, 9, 21, 30, 42}
func (i Pill) String() string {
if i < 0 || i >= Pill(len(_Pill_index)-1) {
return "Pill(" + strconv.FormatInt(int64(i), 10) + ")"
}
return _Pill_name[_Pill_index[i]:_Pill_index[i+1]]
}此时调用fmt.Print(painkiller.Aspirin)将打印字符串“阿司匹林”。
去掉常量名前缀
如果常量名称都包含Pill前缀的话,-trimprefix选项会在生成代码时,去掉Pill前缀。如PillPlacebo、PillAspirin等变量,在String()输出时会去掉前缀。去掉前缀是在生成代码时,去掉前缀,而不是调用String()的时候过滤掉前缀。
//go:generate stringer -type Pill -trimprefix Pill
type Pill int
const (
PillPlacebo Pill = iota // 安慰剂
PillAspirin // 阿司匹林
Ibuprofen // 布洛芬
Paracetamol // 扑热息痛
Acetaminophen = Paracetamol
)总结
提示
stringer 命令只能处理整型的数据类型
