mirror of
https://github.com/chubin/cheat.sheets
synced 2024-11-01 21:40:24 +00:00
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
|