doutuo3899 2012-07-17 21:39
浏览 68
已采纳

为什么在类型开关中不允许掉线?

I'm wondering why fallthrough isn't allowed in a type switch statement in golang.

According to the specification: "The "fallthrough" statement is not permitted in a type switch.", which doesn't explain much about WHY it isn't allowed.

The code attached is to simulate a possible scenario were a fallthrough in a type switch statement might have been useful.

Notice! This code doesn't work, it will produce the error: "cannot fallthrough in type switch". I'm just wondering what possible reasons might have been for not allowing the fallthrough statement in a type switch.

//A type switch question
package main

import "fmt"

//Why isn't fallthrough in type switch allowed?
func main() {
    //Empty interface
    var x interface{}

    x = //A int, float64, bool or string value

    switch i := x.(type) {
    case int:
        fmt.Println(i + 1)
    case float64:
        fmt.Println(i + 2.0)
    case bool:
        fallthrough
    case string:
        fmt.Printf("%v", i)
    default:
        fmt.Println("Unknown type. Sorry!")
    }
}
  • 写回答

1条回答 默认 最新

  • dongrong8972 2012-07-17 21:45
    关注

    How would you expect fallthrough to work? In this type switch, the i variable has a type that depends on the particular case that's invoked. So in the case bool the i variable is typed as bool. But in case string it's typed as string. So either you're asking for i to magically morph its type, which isn't possible, or you're asking for it to be shadowed by a new variable i string, which will have no value because its value comes from x which is not, in fact, a string.


    Here's an example to try and illustrate the problem:

    switch i := x.(type) {
    case int:
        // i is an int
        fmt.Printf("%T
    ", i); // prints "int"
    case bool:
        // i is a bool
        fmt.Printf("%T
    ", i); // prints "bool"
        fallthrough
    case string:
        fmt.Printf("%T
    ", i);
        // What does that type? It should type "string", but if
        // the type was bool and we hit the fallthrough, what would it do then?
    }
    

    The only possible solution would be to make the fallthrough cause the subsequent case expression to leave i as an interface{}, but that would be a confusing and bad definition.

    If you really need this behavior you can already accomplish this with the existing functionality:

    switch i := x.(type) {
    case bool, string:
        if b, ok := i.(bool); ok {
            // b is a bool
        }
        // i is an interface{} that contains either a bool or a string
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求码,CNN+LSTM实现单通道脑电信号EEG的睡眠分期评估
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路