Created
February 1, 2022 10:06
-
-
Save AntonStoeckl/f2bb292ec44f8c4ac86995ade3b94c06 to your computer and use it in GitHub Desktop.
Example for blog post: Go bits: Magic with functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package functionsandinterfaces | |
type ForRegisteringCustomers interface { | |
Register(name, emailAddress string) error | |
} | |
type ForRegisteringCustomersFunc func(name, emailAddress string) error | |
// Register is a receiver method for the ForRegisteringCustomersFunc | |
// type that fullfills the ForRegisteringCustomers interface | |
func (f ForRegisteringCustomersFunc) Register( | |
name string, | |
emailAddress string, | |
) error { | |
return f(name, emailAddress) | |
} | |
// RegisterCustomer is just a plain function that can be type-casted | |
// to ForRegisteringCustomersFunc | |
func RegisterCustomer(name, emailAddress string) error { | |
// do something useful | |
_, _ = name, emailAddress | |
return nil | |
} | |
// CustomerRegistrationHTTPHandler has a dependency to something | |
// that fulfills the ForRegisteringCustomers interface | |
type CustomerRegistrationHTTPHandler struct { | |
forRegisteringCustomers ForRegisteringCustomers | |
} | |
// NewCustomerRegistrationHTTPHandler is just a factory method | |
func NewCustomerRegistrationHTTPHandler( | |
forRegisteringCustomers ForRegisteringCustomers, | |
) *CustomerRegistrationHTTPHandler { | |
return &CustomerRegistrationHTTPHandler{ | |
forRegisteringCustomers: forRegisteringCustomers, | |
} | |
} | |
// Handle uses the ForRegisteringCustomers dependency | |
func (h *CustomerRegistrationHTTPHandler) Handle( | |
name string, | |
emailAddress string, | |
) error { | |
return h.forRegisteringCustomers.Register(name, emailAddress) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment