gpt4 book ai didi

interface - 在 Go 中的结构中使用接口(interface)

转载 作者:IT王子 更新时间:2023-10-29 01:49:27 25 4
gpt4 key购买 nike

在试图理解 Go 时,我在 websocket.go 中遇到了这段代码(已截取):

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
}

在 Conn 类型中,frameHandler 独自站在那里?没有名字的接口(interface)?后来在代码中,他们甚至尝试检查糟糕的接口(interface)是否为 nil:

Conn(a).frameHandler == nil

我自己的猜测是结构中的 frameHandler 是一个与 frameHandler 接口(interface)匹配的类型,并且在其之上将具有名称 frameHandler。它是否正确?呵呵,无论如何都是有趣的语言。

最佳答案

这一行:

    frameHandler

大致等同于:

    frameHandler frameHandler

frameHandler 中,既是字段的名称,也是它的类型。此外,它将frameHandler的所有字段和方法添加到Conn中,所以如果conn是一个Conn,则conn.WriteClose(0)表示conn.frameHandler.WriteClose(0)

作为the Go Programming Language Specification说:

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.T4struct {    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"}

关于interface - 在 Go 中的结构中使用接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9413281/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com