doutao6653 2014-02-26 08:47
浏览 94
已采纳

在交换机中使用时,关键字类型是什么意思?

I have seen several instances of this code in golang:

func process(node ast.Node) Foo {
    switch n := node.(type) {
        // ... removed for brevity
    }
}

ast.Node is an interface. Is the node.(type) snippet code to perform reflection; to find out the actual implementers of the interface ?

  • 写回答

2条回答 默认 最新

  • duanganleng0577 2014-02-26 08:48
    关注

    Yes. It's called a Type switch. It allows you to execute code depending on the actual type of the interface you pass.

    I think the official documentation, with its example, is clear :

    A switch can also be used to discover the dynamic type of an interface variable. Such a type switch uses the syntax of a type assertion with the keyword type inside the parentheses. If the switch declares a variable in the expression, the variable will have the corresponding type in each clause. It's also idiomatic to reuse the name in such cases, in effect declaring a new variable with the same name but a different type in each case.

    var t interface{}
    t = functionOfSomeType()
    switch t := t.(type) {
    default:
        fmt.Printf("unexpected type %T", t)       // %T prints whatever type t has
    case bool:
        fmt.Printf("boolean %t
    ", t)             // t has type bool
    case int:
        fmt.Printf("integer %d
    ", t)             // t has type int
    case *bool:
        fmt.Printf("pointer to boolean %t
    ", *t) // t has type *bool
    case *int:
        fmt.Printf("pointer to integer %d
    ", *t) // t has type *int
    }
    

    You should not use that too often in a properly typed program but it's convenient when you need it. An example of use : Suppose you implement a database driver, you may have to do conversions depending on the type of the Go variables. Here's an extract of the go-sql/mysql driver :

    // Scan implements the Scanner interface.
    // The value type must be time.Time or string / []byte (formatted time-string),
    // otherwise Scan fails.
    func (nt *NullTime) Scan(value interface{}) (err error) {
        if value == nil {
            nt.Time, nt.Valid = time.Time{}, false
            return
        }
    
        switch v := value.(type) {
        case time.Time:
            nt.Time, nt.Valid = v, true
            return
        case []byte:
            nt.Time, err = parseDateTime(string(v), time.UTC)
            nt.Valid = (err == nil)
            return
        case string:
            nt.Time, err = parseDateTime(v, time.UTC)
            nt.Valid = (err == nil)
            return
        }
    
        nt.Valid = false
        return fmt.Errorf("Can't convert %T to time.Time", value)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?