Is it possible to define a method literal? Something like the code below but working.
type Fn struct{
sw Dialer
}
var x fn
fn := func (x Fn) Reboot() error { }
Is it possible to define a method literal? Something like the code below but working.
type Fn struct{
sw Dialer
}
var x fn
fn := func (x Fn) Reboot() error { }
No, but you can use a "function value", so that it can be assigned later on. A common one in network related packages is for a struct to have a Dial func(network, addr string) (net.Conn, error)
field that can be replaced with a custom dialer (e.g. http.Transport)
type Fn struct {
Reboot func() error
}
func main() {
f := Fn{}
f.Reboot = func() error {
return nil
}
f.Reboot()
}