模拟枚举类型
go语言中虽然没有Java, C++等一些高级语言提供的枚举类型特性,但可以使用常量和iota来模拟。 例如:
1// BuildResult 构建结果
2type BuildResult int
3
4const (
5 // Success 成功
6 Success BuildResult = iota
7 // Fail 失败
8 Fail
9 // Abort 中断
10 Abort
11)
1var buildResult = Success
2println(buildResult) // 0
使用go generate和stringer生成String() string函数
https://godoc.org/golang.org/x/tools/cmd/stringergo generatefunc (t T) String() stringfmt.Stringer//go:generate stringer -type=BuildResult
1// BuildResult 构建结果
2//go:generate stringer -type=BuildResult
3type BuildResult int
4
5const (
6 // Success 成功
7 Success BuildResult = iota
8 // Fail 失败
9 Fail
10 // Abort 中断
11 Abort
12)
go get -u golang.org/x/tools/cmd/stringer$GOPATH/bin$PATH
go generatebuildresult_string.go
1// Code generated by "stringer -type=BuildResult"; DO NOT EDIT.
2
3package enums
4
5import "strconv"
6
7func _() {
8 // An "invalid array index" compiler error signifies that the constant values have changed.
9 // Re-run the stringer command to generate them again.
10 var x [1]struct{}
11 _ = x[Success-0]
12 _ = x[Fail-1]
13 _ = x[Abort-2]
14}
15
16const _BuildResult_name = "SuccessFailAbort"
17
18var _BuildResult_index = [...]uint8{0, 7, 11, 16}
19
20func (i BuildResult) String() string {
21 if i < 0 || i >= BuildResult(len(_BuildResult_index)-1) {
22 return "BuildResult(" + strconv.FormatInt(int64(i), 10) + ")"
23 }
24 return _BuildResult_name[_BuildResult_index[i]:_BuildResult_index[i+1]]
25}
使用生成的源码:
1var buildResult = Success
2println(buildResult.String()) // Success