You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Bad.
type Doer interface { Do() Doer }
type IDoer struct{}
func New() Doer { return new(IDoer)}
func (d *IDoer) Do() Doer {/*...*/ return d }
// Good.
type Doer interface { Do() Doer }
type IDoer struct{}
func New() *IDoer { return new(IDoer)}
func (d *IDoer) Do() *IDoer {/*...*/ return d}
// Very Good (Verify Interface Compliance in compile time)
var _ Doer = (*IDoer)(nil)
The above won't compile because of the error:
cannot use (*IDoer)(nil) (value of type *IDoer) as Doer value in variable declaration: *IDoer does not implement Doer (wrong type for method Do)
have Do() *IDoer
want Do() Doer
This use case is quite common designing libraries. As an example, standard slog package has this interface, that's Handler methods return the Handler interface.
How to deal with the case below?
The above won't compile because of the error:
This use case is quite common designing libraries. As an example, standard slog package has this interface, that's Handler methods return the Handler interface.
The text was updated successfully, but these errors were encountered: