The go way to compare values against an enum

135 views
Skip to first unread message

Sankar

unread,
Apr 15, 2019, 8:52:52 AM4/15/19
to golang-nuts
Hi,

I have the following Go type.

```
type weekday string

const (
  Monday weekday = "Monday"
  Tuesday = "Tuesday"
  .
  .
  .
)
```

Now I have a function that receives a string parameter and I want to evaluate if it s a valid weekday or not. Say:

```
func handler(w http.ResponseWriter, r *http.Request) {

// Parse req.body and get a day string

day := "Tuesday" // Valid day
day := "tuesday" // Invalid day
day := "123819283" // Invalid day

}
```

I want to evaluate if the string variable `day` is one of the valid permitted values for the [Ww]eekday custom type. What is the recommended / right go way of doing this ?

Thanks.

roger peppe

unread,
Apr 15, 2019, 9:00:34 AM4/15/19
to Sankar, golang-nuts
Go doesn't have enum types. For any given type, even if there are constants of that type, that doesn't mean that those are all the valid values of that type. For that reason, Go doesn't provide any specific functionality to do what you'd like.

I'd suggest creating a map that holds all the valid weekday names:

        var weekdays = map[string]bool{
                "Monday": true,
                "Tuesday": true,
                ... etc
        }
        
        func IsValidWeekday(s string) bool {
                return weekdays[s]
        }

There might not be any particular advantage to creating a specific weekday type in this case.
That might be different if you were going to use an integer representation (as the time package does, for example).

Hope this helps,
  rog.


--
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...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jan Mercl

unread,
Apr 15, 2019, 9:01:34 AM4/15/19
to Sankar, golang-nuts


On Mon, Apr 15, 2019 at 10:53 AM Sankar <sankar.c...@gmail.com> wrote:

> I want to evaluate if the string variable `day` is one of the valid permitted values for the [Ww]eekday custom type. What is the recommended / right go way of doing this ?

Go has no enums and type weekday has no permitted/not permitted values.

You have to check for valid values manually. Sometimes using a map is a good approach, sometimes a switch may be more appropriate. 

For numeric intervals an if statement will work best so sometimes it's better to have symbolic names for the permitted values and use the stringer utility to attach a String() method to them.

type Weekday int // or perhaps uint

const (
        Monday Weekday = iota
        Tuiesday
        Wednesday
        ...
        Sunday
)

Sankar P

unread,
Apr 15, 2019, 9:51:54 AM4/15/19
to roger peppe, Jan Mercl, golang-nuts
thanks guys.

திங்., 15 ஏப்., 2019, பிற்பகல் 2:30 அன்று, roger peppe <rogp...@gmail.com> எழுதியது:


--

Marvin Renich

unread,
Apr 15, 2019, 4:24:18 PM4/15/19
to golang-nuts
Roger and Jan have given you good answers, but I would like to comment
on another aspect of your code.

* Sankar <sankar.c...@gmail.com> [190415 04:53]:
> type weekday string
>
> const (
> Monday weekday = "Monday"
> Tuesday = "Tuesday"
> .
> .
> .
> )

Note that the type of Monday will be weekday, while the type of Tuesday
will be untyped string. The spec allows omitting the expression list
after the first const declaration in a parenthesized list, but if you
omit the type, but not the expression list, the type is implied by the
expression, not by the type of the previous const.

...Marvin

Sankar P

unread,
Apr 15, 2019, 4:26:06 PM4/15/19
to golang-nuts, mr...@renich.org
Whoa. I did not realise this. Thanks for the help.

திங்., 15 ஏப்., 2019, பிற்பகல் 9:54 அன்று, Marvin Renich <mr...@renich.org> எழுதியது:
--
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/cs1DE0jOOfA/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.

Tong Sun

unread,
Apr 16, 2019, 6:26:57 PM4/16/19
to golang-nuts
Roger and Jan have given you good answers, but if you want to make use of an existing small package, check out 

Reply all
Reply to author
Forward
0 new messages