在 golang 中显示一周的第二天

Show the next day of the week in golang

我刚开始学习 IT,我认为制作一个小程序来告诉您明天是星期几会很有趣。可悲的是我被困住了。目前,当您从数组中写入正确的数字时它可以工作,但我希望它可以使用字符串。因此,当您输入 'Maandag'(荷兰语为星期一)时,程序将回答 Dinsdag(荷兰语为星期二)

到目前为止,这是我的代码:

package main

import (
    "fmt"
)

func main() {

    var counter int

    var dag [7]string
    dag[0] = "Zondag"
    dag[1] = "Maandag"
    dag[2] = "Dinsdag"
    dag[3] = "Woensdag"
    dag[4] = "Donderdag"
    dag[5] = "Vrijdag"
    dag[6] = "Zaterdag"

    fmt.Println("Welke dag is het?")
    fmt.Scan(&counter)

    if counter == 6 {
        counter = 0
        fmt.Println(dag[counter])
    }

    if counter != 6 {
        counter++
        fmt.Println(dag[counter])
    }
}

遍历天数数组以获得其索引值。检查传递的字符串并获取匹配字符串索引的值。然后使用该索引获取第二天的值:

package main

import (
    "fmt"
)

var (
    counter int
    day     string
)

func main() {

    var dag [7]string
    dag[0] = "Zondag"
    dag[1] = "Maandag"
    dag[2] = "Dinsdag"
    dag[3] = "Woensdag"
    dag[4] = "Donderdag"
    dag[5] = "Vrijdag"
    dag[6] = "Zaterdag"

    fmt.Println("Welke dag is het?")
    fmt.Scan(&day)
    for key, value := range dag {
        if day == value {
            counter = key
        }
    }
    fmt.Println(counter)
    if counter == 6 {
        counter = 0
        fmt.Println(dag[counter])
    }

    if counter != 6 {
        counter = counter + 1
        fmt.Println(dag[counter])
    }
}

Playground Example

或者按照 Peter 的建议,您也可以使用地图,在这种情况下更方便易用:

package main

import (
    "fmt"
)

func main() {

    var value string
    dag := make(map[string]string)
    dag["Zondag"] = "Maandag"
    dag["Maandag"] = "Dinsdag"
    dag["Dinsdag"] = "Woensdag"
    dag["Woensdag"] = "Donderdag"
    dag["Donderdag"] = "Vrijdag"
    dag["Vrijdag"] = "Zaterdag"

    fmt.Println("Welke dag is het?")
    fmt.Scan(&value)
    fmt.Println(dag[value])
}

Go Playground

上的工作代码

可以枚举数组,传递数字和字符串

func GetNextDay(someday string){
    for i, v := range dag{
      if v == someday {
          if i==6 {
              i = 0
          }else {
             i = i + 1
          }
          return dag[i]
      }
    }
    return "no such day in a week"
}

你要找的是枚举。在 Go 中,它们可以这样实现:

type Weekday int 

const (
   Sunday    Weekday = iota
   Monday    
   Tuesday   
   Wednesday 
   Thursday  
   Friday    
   Saturday   
)

func (day Weekday) String() string {
    // declare an array of strings
    // ... operator counts how many
    // items in the array (7)
    names := [...]string{
        "Sunday", 
        "Monday", 
        "Tuesday", 
        "Wednesday",
        "Thursday", 
        "Friday", 
        "Saturday"}
    // → `day`: It's one of the
    // values of Weekday constants.    
    // If the constant is Sunday,
    // then day is 0.
    // prevent panicking in case of
    // `day` is out of range of Weekday
    if day < Sunday || day > Saturday {
      return "Unknown"
    }
    // return the name of a Weekday
    // constant from the names array 
    // above.
    return names[day]
}

// will display "Sunday"
fmt.Println(Sunday)

// will display "Monday"
fmt.Println(Sunday + 1)

如果不需要int底层类型,可以这样创建:

const (
    Sunday = "Sunday"
     //...
)