enum

command module
v0.0.0-...-838b099 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2023 License: MIT Imports: 2 Imported by: 0

README

Enum

An example showing the implementation of enum.

Basics

This example includes a method for returning the enum value as string. In the String method, we create an array of string values and use the enum value, which is an integer type to index the actual string value.

See Stackoverflow: how to make go print enum fields as string for details. You can also implement the method using switch to return the string values per Rishabh's comment.

Using Stringer

We can use also use a codegen tool stringer to generate the method String() of an enum type definition for us. See the type MyEnumValImplict. We need to do the following:

go install golang.org/x/tools/cmd/stringer@latest
go generate
go run main.go myenumimplicit_string.go

Or simply run make run.

References

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

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