Iota with string constants

3,510 views
Skip to first unread message

m...@inanc.io

unread,
Oct 15, 2017, 4:13:35 PM10/15/17
to golang-nuts
Is it anything wrong just assigning string values to get rid of String() method attaching?

Like this:

type Weekday string

const (
 
Sunday Weekday = "Sunday"
 
Monday Weekday = "Monday"
)

func main() {
  fmt.Println(Sunday)
}



Instead of this:

type weekday uint

const (
  Sunday weekday = iota
  Monday
)

var weekdayNames = [...]string{"Sunday", "Monday"}

func (day weekday) String() string {
  return weekdayNames[day]
}


func main() {
  fmt.Println(Sunday)
}



Thanks.

Marvin Stenger

unread,
Oct 15, 2017, 7:45:35 PM10/15/17
to golang-nuts

Inanc Gumus

unread,
Oct 15, 2017, 8:12:14 PM10/15/17
to Marvin Stenger, golang-nuts
Yep, I know that one, I’m asking about the convention, idioms and the performance-wise.

Inanc Gumus

Marvin Stenger <marvin.s...@gmail.com> şunları yazdı (16 Eki 2017 02:45):

--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/mbHdiXkb7iU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Diego Medina

unread,
Oct 16, 2017, 11:16:52 AM10/16/17
to golang-nuts
If it is something like weekdays (very few options), I don't see any issues with it. Stringer is great when as you go along using your app, you may need to add more values over time.

using iota and stringer is great when your list may be 15 or more items, and keeping them updated becomes a chore you would like to avoid.

Regards,

Diego

Inanc Gumus

unread,
Oct 16, 2017, 11:38:26 AM10/16/17
to Diego Medina, golang-nuts
Thx. Btw, I don't know about any inlining help from the Go compiler when you use iota or a string inside constants. 

To unsubscribe from this group and all its topics, send an email to golang-nuts+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
I've been a programmer since 1993 and an online teacher and mentor since 2016. Now, I'm working on full-time from my homeoffice to teach you Go.

 @inancgumus
📚 Learn Go Programming Blog

Josh Humphries

unread,
Oct 16, 2017, 11:55:58 AM10/16/17
to m...@inanc.io, golang-nuts
Comparing values will be a lot cheaper with ints than strings. So if your enum type is possibly used in hot loops, ints will be your friend. Also, with ints, you can provide an ordering that differs from the lexical order of the string values. For example, with an int, you can easily sort by day-of-week by defining the constants in order. With strings, you have to use a custom compare function that acts on string names (ick).

----
Josh Humphries
jh...@bluegosling.com

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages