4

If I have an enum:

type Day int8

const (
    Monday Day = iota
    Tuesday
    ...
    Sunday
)

What is more natural Go way to get string of it?

fucntion:

func ToString(day Day) string {
   ...
}

or method

func (day Day) String() string  {
    ... 
}
Sergii Getman
  • 3,845
  • 5
  • 34
  • 50
  • 1
    Check this out: https://blog.golang.org/generate It may be more apt for your use case. – ssemilla Oct 23 '18 at 08:49
  • Possible duplicate of [What is an idiomatic way of representing enums in Go?](https://stackoverflow.com/questions/14426366/what-is-an-idiomatic-way-of-representing-enums-in-go) – Jonathan Hall Oct 23 '18 at 09:15

3 Answers3

11

The second one is more idiomatic because it satisfies Stringer interface.

func (day Day) String() string  {
    ... 
}

We declare this method on the Day type not *Day type because we are not changing the value.

It will enable you to write.

fmt.Println(day)

and get the value produced by String method.

Grzegorz Żur
  • 47,257
  • 14
  • 109
  • 105
  • Thanks, Grzegorz Żur. I follow that way. But there is another point of view: if we don't change type state - better to declare function. – Sergii Getman Oct 23 '18 at 09:02
  • @SergiiGetman Could explain this part better "we don't change type state"? – Grzegorz Żur Oct 23 '18 at 09:17
  • Grzegorz Żur. I ma newbie in go and can be wrong. But idea is if we have type, for example `struct`. and we have some piece of code that change that struct it is better to make it method of that type. if we just `use` the type we can make fucntion – Sergii Getman Oct 23 '18 at 09:32
  • Please see [Should I define methods on values or pointers?](https://golang.org/doc/faq#methods_on_values_or_pointers) – Grzegorz Żur Oct 23 '18 at 09:46
7

The easy way for you to answer this question yourself is to look at the Go standard library.


Package time

import "time" 

type Weekday

A Weekday specifies a day of the week (Sunday = 0, ...).

type Weekday int

const (
        Sunday Weekday = iota
        Monday
        Tuesday
        Wednesday
        Thursday
        Friday
        Saturday
)

func (Weekday) String

func (d Weekday) String() string

String returns the English name of the day ("Sunday", "Monday", ...).

src/time/time.go:

// A Weekday specifies a day of the week (Sunday = 0, ...).

type Weekday int

const (
    Sunday Weekday = iota
    Monday
    Tuesday
    Wednesday
    Thursday
    Friday
    Saturday
)

var days = [...]string{
    "Sunday",
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday",
}

// String returns the English name of the day ("Sunday", "Monday", ...).
func (d Weekday) String() string {
    if Sunday <= d && d <= Saturday {
        return days[d]
    }
    buf := make([]byte, 20)
    n := fmtInt(buf, uint64(d))
    return "%!Weekday(" + string(buf[n:]) + ")"
}

Package fmt

import "fmt" 

type Stringer

Stringer is implemented by any value that has a String method, which defines the “native” format for that value. The String method is used to print values passed as an operand to any format that accepts a string or to an unformatted printer such as Print.

type Stringer interface {
        String() string
}
peterSO
  • 158,998
  • 31
  • 281
  • 276
1

Maybe my answer might have performance hit but when working with a huge set of enums, having a mapping would be a terrible ideatype Category string

type Category string

const (
    AllStocks        Category = "all"
    WatchList        Category = "watch_list"
    TopGainer        Category = "top_gainer_stock"
    TopLoser         Category = "top_loser_stock"
    FiftyTwoWeekHigh Category = "high_stocks"
    FiftyTwoWeekLow  Category = "low_stocks"
    HotStocks        Category = "hot_stock"
    MostTraded       Category = "most_active_stock"
)

func (c Category) toString() string {
    return fmt.Sprintf("%s", c)
}

This is the easiest string formatting route for enums.

mourya venkat
  • 267
  • 3
  • 10