doushan2224 2017-06-02 08:58
浏览 646
已采纳

Golang游览Switch评估顺序:time.Now()。Weekday()+ 2产生运行时错误:索引超出范围

I am learning Golang, was going through the tour where I found a tutorial on Switch evaluation order. I modified it a bit (e.g. Saturday to Sunday), just to play around. It printed Too far away. even for Sunday. So, I modified the code to look like this:

package main

import (
    "fmt"
    "time"
)

func main() {
    day := time.Monday

    fmt.Printf("When's %v?
", day)
    today := time.Now().Weekday()

    switch day {
    case today + 0:
        fmt.Println("Today.")
    case today + 1:
        fmt.Println("Tomorrow.", today + 1)
    case today + 2:
        fmt.Println("In two days.", today + 2)
    default:
        fmt.Println("Too far away.", today + 2)
    }
}

Now, it gives me the output:

When's Monday?
Too far away. %!v(PANIC=runtime error: index out of range)

What can I do to MOD the index, instead of adding it beyond array? Seems to me like some kind of operator overloading. Shouldn't it do MOD, on add operation, by default in case of days, at least?

  • 写回答

1条回答 默认 最新

  • dongzhimin2231 2017-06-02 09:11
    关注

    This is an implementation detail.

    In this line

    fmt.Println("In two days.", today + 2)
    

    today is of type time.Weekday which has int as its underlying type, 2 is an untyped integer constant, which will be converted to time.Weekday and the addition will be carried out.

    The implementation of fmt.Println() will check if values passed to it implement fmt.Stringer, and because time.Weekday does, its String() method will be called whose implementation is:

    // String returns the English name of the day ("Sunday", "Monday", ...).
    func (d Weekday) String() string { return days[d] }
    

    Where days is an array of 7 elements:

    var days = [...]string{
        "Sunday",
        "Monday",
        "Tuesday",
        "Wednesday",
        "Thursday",
        "Friday",
        "Saturday",
      }
    

    There is no range check in Weekday.String() because time.Saturday + 2 for example is not a weekday. Weekday.String() only guarantees to work properly for the constants defined in the time package:

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

    If you want to make it work, you have to use the remainder after dividing by 7, like this:

    switch day {
    case (today + 0) % 7:
        fmt.Println("Today.")
    case (today + 1) % 7:
        fmt.Println("Tomorrow.", (today+1)%7)
    case (today + 2) % 7:
        fmt.Println("In two days.", (today+2)%7)
    default:
        fmt.Println("Too far away.", (today+2)%7)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料