This question already has an answer here:
I can understand whole last line of code meaning: It provides a static (compile time) check that *DefaultHandler
satisfies the EasygateHandler
interface. But I cannot understand what's the meaning of (*DefaultHandler)(nil)
and what's the difference with (*DefaultHandler)()
type DefaultHandler struct {
log logrus.FieldLogger
svc *service.DefaultResolver
}
var _ easygate.EasygateHandler = (*DefaultHandler)(nil)
</div>
The expression (*DefaultHandler)(nil)
is a conversion from an untyped nil to a zero value of type *DefaultHandler
.
Conversions are of the form T(x)
where T
is a type and x
is a value that can be converted to type T
. In this example, T
is *DefaultHandler
and x
is nil
. The parenthesis around *DefaultHandler
are required to distinguish the conversion to a pointer type from a dereference of a a conversion to the non-pointer type.
The expression (*DefaultHandler)()
is not valid Go syntax.
The value &DefaultHandler{}
can also be used if DefaultHandler
is a type with composite literal syntax. The conversion pattern works for all types.