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 {
...
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With