Home > Backend Development > Golang > How Do I Convert Weekday Strings to time.Weekday in Go Efficiently?

How Do I Convert Weekday Strings to time.Weekday in Go Efficiently?

Linda Hamilton
Release: 2024-10-31 13:10:34
Original
288 people have browsed it

How Do I Convert Weekday Strings to time.Weekday in Go Efficiently?

Parse Weekday String into time.Weekday: An Idiomatic Approach with a Map

Converting a weekday string into a corresponding time.Weekday value in Go requires a custom solution since there's no built-in functionality for this conversion. While the presented array-based function provides a workable solution, a map-based approach offers advantages in efficiency and flexibility.

Enhanced Solution Using a Map

Instead of using an array, utilizing a map with weekday strings as keys and their corresponding time.Weekday values as values significantly speeds up lookups and simplifies the code:

<code class="go">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)
}</code>
Copy after login

Benefits of the Map-Based Approach

  • Faster lookups: Maps provide O(1) lookup time, making them more efficient than iterating through an array.
  • Straightforward initialization: Initializing a map is easier, especially when done with a for loop, as shown in this example:
<code class="go">var daysOfWeek = map[string]time.Weekday{}

func init() {
    for d := time.Sunday; d <= time.Saturday; d++ {
        daysOfWeek[d.String()] = d
    }
}</code>
Copy after login

Expanding the Map for Additional Input

This map-based approach also allows for the flexible addition of new valid values that can be parsed into time.Weekday. For example, short 3-letter weekday names can be included with a simple loop:

<code class="go">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
    }
}</code>
Copy after login

This extension allows parsing both full weekday names (e.g., "Monday") and short weekday names (e.g., "Mon") into time.Weekday.

Using a map in this context provides a faster, more convenient, and extendable way to parse weekday strings into time.Weekday values in Go.

The above is the detailed content of How Do I Convert Weekday Strings to time.Weekday in Go Efficiently?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template