dtyrxmoj20617 2018-09-12 08:37
浏览 35
已采纳

在golang中显示一周的第二天

I've just started my study IT and I thought it would be fun to make a little program that would show you what day it is tomorrow. Sadly I'm stuck. Currently it's working when you write the correct number from the array, but I would like it to work with a string. So when you write 'Maandag' (monday in Dutch), the program will answer Dinsdag (Tuesday in Dutch)

This is my code so far:

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])
    }
}
  • 写回答

3条回答 默认 最新

  • dpf7891 2018-09-12 09:02
    关注

    What are you looking for are enums. In Go they can be implemented like this:

    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)
    

    If you do not need int underlying type, you can create it like this:

    const (
        Sunday = "Sunday"
         //...
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Android Studio中如何把H5逻辑放在Assets 文件夹中以实现将h5代码打包为apk
  • ¥15 使用小程序wx.createWebAudioContext()开发节拍器
  • ¥15 关于#爬虫#的问题:请问HMDB代谢物爬虫的那个工具可以提供一下吗
  • ¥15 vue3+electron打包获取本地视频属性,文件夹里面有ffprobe.exe 文件还会报错这是什么原因呢?
  • ¥20 用51单片机控制急停。
  • ¥15 孟德尔随机化结果不一致
  • ¥15 在使用pyecharts时出现问题
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0