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 <https://golang.org/pkg/time/#Weekday>, for example).

Hope this helps,
  rog.


On Mon, 15 Apr 2019 at 09:52, Sankar <sankar.curios...@gmail.com> wrote:

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

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

Reply via email to