Hello, I’m fried fish.

In the daily business engineering development, we often have the appeal of using enumeration values, enumeration control is good, test value boundary again…

Iota types are enumerated in Go. What else does this article say about fried fish?

Go does not have the enum keyword. If you have used Protobuf, you will know that Go only supports “limited enumeration”. We will also use constants to define enumeration values.

The sample

In some business scenarios, it’s not possible to achieve what we want. The following is an example:

type FishType int

const (
 A FishType = iota
 B
 C
 D
)

func main() {
 fmt.Println(A, B, C, D)
}
Copy the code

The output is 0 1 2 3. At this time a face meng force… Enumeration values, which should have a corresponding value in addition to the key. What does 0, 1, 2, 3 mean? Should I print “A, B, C, D”?

But there is no direct support for the Go language, so this is not a complete implementation of enumerated types.

Also, if we pass an enumeration value outside the FishType declaration range, there is no control by default in the Go language and it is output normally.

The above implementation of Go enumerations is, in some cases, incomplete and cannot strictly be enums.

Use String for enumeration

To support the corresponding output of enumerated values, we can do the following:

type FishType int const ( A FishType = iota B C D ) func (f FishType) String() string { return [...] string{"A", "B", "C", "D"}[f] }Copy the code

Run the program:

Func main() {var f FishType = A FMT.Println(f) switch f {case A: FMT.Println(f) case B: FMT.Println(" remember to like ") default: FMT.Println(" no no no...") )}}Copy the code

Output result:

A brain is fried in fishCopy the code

We can use the default convention for the String method in Go, which is called on output by default for the type that defines the String method.

In this way, you can get both the enumeration value and the literal meaning of the mapping.

Automatic String generation

But every time you write it manually, it’s a hassle. In this section, we can use the official provided CMD /string for a quick implementation.

We install the following command:

go install golang.org/x/tools/cmd/stringer
Copy the code

Set the go:generate directive on the required enumeration values:

//go:generate stringer -type=FishType
type FishType int
Copy the code

Execute in the project root directory:

go generate ./...

Copy the code

The fishType_String. go file is generated in the root directory:

. ├ ─ ─ fishtype_string. Go ├ ─ ─. Mod ├ ─ ─. Sum └ ─ ─ main. GoCopy the code

Fishtype_string File contents:

package main import "strconv" const _FishType_name = "ABCD" var _FishType_index = [...] uint8{0, 1, 2, 3, 4} func (i FishType) String() string { if i < 0 || i >= FishType(len(_FishType_index)-1) { return "FishType(" + strconv.FormatInt(int64(i), 10) + ")" } return _FishType_name[_FishType_index[i]:_FishType_index[i+1]] }Copy the code

The generated file is mainly based on the enumeration value and the mapping value to do a mapping, and for the scenario beyond the enumeration value is judged:

func main() {
 var f1 FishType = A
 fmt.Println(f1)
 var f2 FishType = E
 fmt.Println(f2)
}
Copy the code

Run the go run. Check the running result of the program:

$ go run .
A
FishType(4)
Copy the code

conclusion

In today’s article, we showed you how to implement standard enumerations in the Go language, which is a bit cumbersome, but not too difficult overall.

I believe that in the future, THERE will be a chance to see Go support enum.

How would you normally implement enumeration in the business code, welcome to leave a comment exchange 🙂

You are welcome to leave your comments in the comments section 🙂

If you have any questions, welcome feedback and communication in the comments section. The best relationship is mutual achievement. Your praise is the biggest motivation for the creation of fried fish, thank you for your support.

This article is constantly updated. You can read it on wechat by searching “Brain into fried fish”. GitHub github.com/eddycjy/blo… Already included, learning Go language can see Go learning map and route, welcome Star urged more.