Parse Weekday string into time.Weekday

Foreword: I released this utility in github.com/icza/gox, see timex.ParseWeekday().


Yes, use a map instead of an array, so lookups are faster and more straight-forward:

var daysOfWeek = map[string]time.Weekday{
    "Sunday":    time.Sunday,
    "Monday":    time.Monday,
    "Tuesday":   time.Tuesday,
    "Wednesday": time.Wednesday,
    "Thursday":  time.Thursday,
    "Friday":    time.Friday,
    "Saturday":  time.Saturday,
}

func parseWeekday(v string) (time.Weekday, error) {
    if d, ok := daysOfWeek[v]; ok {
        return d, nil
    }

    return time.Sunday, fmt.Errorf("invalid weekday '%s'", v)
}

Testing it:

fmt.Println(parseWeekday("Monday"))
fmt.Println(parseWeekday("Friday"))
fmt.Println(parseWeekday("invalid"))

Output (try it on the Go Playgorund):

Monday <nil>
Friday <nil>
Sunday invalid weekday 'invalid'

Tip:

You can even use a for loop to initialize safely the daysOfWeek map like this:

var daysOfWeek = map[string]time.Weekday{}

func init() {
    for d := time.Sunday; d <= time.Saturday; d++ {
        daysOfWeek[d.String()] = d
    }
}

Testing and output is the same. Try this one on the Go Playground.

Another nice property of this map-solution (compared to your array-solution) is that you may list additional valid values in the same map that may be parsed into time.Weekday without additional parsing code.

For example, let's also parse the 3-letter short weekday names into their time.Weekday equivalent, e.g. "Mon" to time.Monday.

This extension can be added with a simple loop:

var daysOfWeek = map[string]time.Weekday{}

func init() {
    for d := time.Sunday; d <= time.Saturday; d++ {
        name := d.String()
        daysOfWeek[name] = d
        daysOfWeek[name[:3]] = d
    }
}

Testing it:

fmt.Println(parseWeekday("Monday"))
fmt.Println(parseWeekday("Friday"))
fmt.Println(parseWeekday("Mon"))
fmt.Println(parseWeekday("Fri"))
fmt.Println(parseWeekday("invalid"))

Output (try it on the Go Playground):

Monday <nil>
Friday <nil>
Monday <nil>
Friday <nil>
Sunday invalid weekday 'invalid'

See similar question: Get integer month value from string

Tags:

Time

String

Go