douliang1900 2016-04-22 22:44
浏览 39
已采纳

Go的功能是什么,看起来像演员一样[重复]

This question already has an answer here:

Can someone explain what the following syntax means in Go, specifially:

x.([]byte)

I am not sure what this means, is this a cast? But for what method since it is just after a dot?

func of(x interface{}) ByteView {
    if bytes, ok := x.([]byte); ok {
        return ByteView{b: bytes}
    }
    return ByteView{s: x.(string)}
}

Reference: https://github.com/golang/groupcache/blob/master/byteview_test.go#L55

</div>
  • 写回答

1条回答 默认 最新

  • dongzhan1383 2016-04-22 23:03
    关注

    We refer to this as "type assertion."

    This is perfectly documented in the Language Spec. In order to have the whole answer here on SO (since it is the whole answer) and not a link-only answer, I will include it the most relevant information from the spec. This is documentation, not my answer...

    For an expression x of interface type and a type T, the primary expression

    x.(T)
    

    asserts that x is not nil and that the value stored in x is of type T. The notation x.(T) is called a type assertion.

    More precisely, if T is not an interface type, x.(T) asserts that the dynamic type of x is identical to the type T. In this case, T must implement the (interface) type of x; otherwise the type assertion is invalid since it is not possible for x to store a value of type T. If T is an interface type, x.(T) asserts that the dynamic type of x implements the interface T.

    If the type assertion holds, the value of the expression is the value stored in x and its type is T. If the type assertion is false, a run-time panic occurs. In other words, even though the dynamic type of x is known only at run time, the type of x.(T) is known to be T in a correct program.

    var x interface{} = 7  // x has dynamic type int and value 7 
    i := x.(int)           // i has type int and value 7
    
    type I interface { m() }
    var y I
    s := y.(string)        // illegal: string does not implement I (missing method m) 
    r := y.(io.Reader)     // r has type io.Reader and y must implement both I and io.Reader 
    

    A type assertion used in an assignment or initialization of the special form

    v, ok = x.(T) 
    v, ok := x.(T)
    var v, ok = x.(T) 
    

    yields an additional untyped boolean value. The value of ok is true if the assertion holds. Otherwise it is false and the value of v is the zero value for type T. No run-time panic occurs in this case.

    Effective Go is another great resource, which also includes a section on Interface conversions and type assertions: https://golang.org/doc/effective_go.html#interface_conversions

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致