You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
24 lines
650 B
Plaintext
24 lines
650 B
Plaintext
// There is no subclassing in Go. Instead, there is interface and struct embedding.
|
|
|
|
// ReadWriter implementations must satisfy both Reader and Writer
|
|
type ReadWriter interface {
|
|
Reader
|
|
Writer
|
|
}
|
|
|
|
// Server exposes all the methods that Logger has
|
|
type Server struct {
|
|
Host string
|
|
Port int
|
|
*log.Logger
|
|
}
|
|
|
|
// initialize the embedded type the usual way
|
|
server := &Server{"localhost", 80, log.New(...)}
|
|
|
|
// methods implemented on the embedded struct are passed through
|
|
server.Log(...) // calls server.Logger.Log(...)
|
|
|
|
// the field name of the embedded type is its type name (in this case Logger)
|
|
var logger *log.Logger = server.Logger
|