-
To ensure interface cannot be implemented in other packages, add a private function (first character must be lower-case) named "is" + type name, which takes no arguments, returns nothing, and has an empty body.
For example:
type I interface { isI() }
-
To ensure a type implements an interface at compile-time, use the "interface guard" pattern: Introduce a global variable named
_
, type it as the interface, and assign an empty value of the concrete type to it.For example:
type T struct { //... } var _ io.ReadWriter = (*T)(nil) func (t *T) Read(p []byte) (n int, err error) { // ...
See