donglan7594 2012-02-23 12:41
浏览 6
已采纳

在Go中使用结构中的接口

In trying to understand Go, I ran into this piece of code in websocket.go (snipped):

type frameHandler interface {
    HandleFrame(frame frameReader) (r frameReader, err error)
    WriteClose(status int) (err error)
}

// Conn represents a WebSocket connection.
type Conn struct {
    config  *Config
    request *http.Request
    .
    .
    frameHandler
    PayloadType        byte
    defaultCloseStatus int
}

In the Conn type the frameHandler stands there all alone? An interface without a name? Later on in the code they even try check if the poor interface is nil:

Conn(a).frameHandler == nil

My own guess is that the frameHandler within the struct is a type which matches the frameHandler interface, and on top of that will have the name frameHandler. Is this correct? Hehe, fun language anyhow.

  • 写回答

1条回答 默认 最新

  • duancha1065 2012-02-23 12:45
    关注

    This line:

        frameHandler
    

    is roughly equivalent to this:

        frameHandler frameHandler
    

    in that frameHandler is both the name of the field and its type. In addition, it adds all the fields and methods of the frameHandler to the Conn, so if conn is a Conn, then conn.WriteClose(0) means conn.frameHandler.WriteClose(0).

    As the Go Programming Language Specification puts it:

    A field declared with a type but no explicit field name is an anonymous field (colloquially called an embedded field). Such a field type must be specified as a type name T or as a pointer to a non-interface type name *T, and T itself may not be a pointer type. The unqualified type name acts as the field name.

    // A struct with four anonymous fields of type T1, *T2, P.T3 and *P.T4
    struct {
        T1        // field name is T1
        *T2       // field name is T2
        P.T3      // field name is T3
        *P.T4     // field name is T4
        x, y int  // field names are x and y
    }
    The following declaration is illegal because field names must be unique in a struct type:
    struct {
        T         // conflicts with anonymous field *T and *P.T
        *T        // conflicts with anonymous field T and *P.T
        *P.T      // conflicts with anonymous field T and *T
    }
    Fields and methods (§Method declarations) of an anonymous field are promoted to be ordinary fields and methods of the struct (§Selectors). The following rules apply for a struct type named S and a type named T:
    • If S contains an anonymous field T, the method set of S includes the method set of T.
    • If S contains an anonymous field *T, the method set of S includes the method set of *T (which itself includes the method set of T).
    • If S contains an anonymous field T or *T, the method set of *S includes the method set of *T (which itself includes the method set of T).
    A field declaration may be followed by an optional string literal tag, which becomes an attribute for all the fields in the corresponding field declaration. The tags are made visible through a reflection interface but are otherwise ignored.
    // A struct corresponding to the TimeStamp protocol buffer.
    // The tag strings define the protocol buffer field numbers.
    struct {
        microsec  uint64 "field 1"
        serverIP6 uint64 "field 2"
        process   string "field 3"
    }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图