Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Go print enum fields as string?

Tags:

enums

go

You print an enum that implements Stringer using "%v", it will print its string value. If you declare the same enum inside a struct and print the struct using "%v", it will print enum's numeric value. Is there a way to print the string value of a enum field?

Sample (https://play.golang.org/p/AP_tzzAZMI):

package main  import (     "fmt" )  type MyEnum int  const (     Foo MyEnum = 1     Bar MyEnum = 2 )  func (e MyEnum) String() string {     switch e {     case Foo:         return "Foo"     case Bar:         return "Bar"     default:         return fmt.Sprintf("%d", int(e))     } }  type MyStruct struct {     field MyEnum }  func main() {     info := &MyStruct{         field: MyEnum(1),     }     fmt.Printf("%v\n", MyEnum(1))     fmt.Printf("%v\n", info)     fmt.Printf("%+v\n", info)     fmt.Printf("%#v\n", info) } 

Prints:

Foo &{1} &{field:1} &main.MyStruct{field:1} 
like image 439
Igor Gatis Avatar asked Jan 05 '17 08:01

Igor Gatis


People also ask

How do I convert an enum to a string?

There are two ways to convert an Enum to String in Java, first by using the name() method of Enum which is an implicit method and available to all Enum, and second by using toString() method.

Can we use enum as string?

In a string enum, each member has to be constant-initialized with a string literal, or with another string enum member. While string enums don't have auto-incrementing behavior, string enums have the benefit that they “serialize” well.

How do I declare a string enum in Golang?

String Enums in Go Go doesn't have any built-in string functionality for enums, but it's pretty easy to implement a String() method. By using a String() method instead of setting the constants themselves as string types, you can get the same benefits of an enum with the “printability” of a string.


2 Answers

You need to make the field exported,ie you may declare the struct as

type MyStruct struct {     Field MyEnum } 

Here is a sample program with exported and unexported fields

Code

package main  import (     "fmt" )  type MyEnum int  const (     Foo MyEnum = 1     Bar MyEnum = 2 )  func (e MyEnum) String() string {     switch e {     case Foo:         return "Foo"     case Bar:         return "Bar"     default:         return fmt.Sprintf("%d", int(e))     } }  type MyStruct struct {     Field1 MyEnum     field2 MyEnum }  func main() {     info := &MyStruct{         Field1: MyEnum(1),         field2: MyEnum(2),     }     fmt.Printf("%v\n", MyEnum(1))     fmt.Printf("%v\n", info)     fmt.Printf("%+v\n", info)     fmt.Printf("%#v\n", info) } 

Output

Foo &{Foo 2} &{Field1:Foo field2:2} &main.MyStruct{Field1:1, field2:2} 

Here is play link : https://play.golang.org/p/7knxM4KbLh

like image 73
Sarath Sadasivan Pillai Avatar answered Oct 05 '22 02:10

Sarath Sadasivan Pillai


I am going to expand on the accepted answer a little with this method:

type MyEnum int  const (     Foo MyEnum = iota     Bar )  func (me MyEnum) String() string {     return [...]string{"Foo", "Bar"}[me] }  // ...  fmt.Println(Foo, Bar)  // Prints: Foo Bar 

The above code assumes that the enum values starts from 0, which works out nicely because the first element in the array in the method String can be referenced by the enum value directly.

But the first enum value in the original question has a value of 1. We can modify it the method accordingly.

const (     Foo MyEnum = iota + 1     Bar )  func (me MyEnum) String() string {     return [...]string{"", "Foo", "Bar"}[me] } 

Here's the playlink: https://play.golang.org/p/6pmyVlsAeV2

like image 23
Cybersam Avatar answered Oct 05 '22 03:10

Cybersam