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.
This line:
is roughly equivalent to this:
in that
frameHandleris both the name of the field and its type. In addition, it adds all the fields and methods of theframeHandlerto theConn, so ifconnis aConn, thenconn.WriteClose(0)meansconn.frameHandler.WriteClose(0).As the Go Programming Language Specification puts it: