Sobes.tech
Junior — Middle

How can you verify that a data structure implements the required interface?

sobes.tech AI

Answer from AI

In Go, to check that a structure implements an interface, you can use an implicit implementation — it is enough for the structure to have all the methods of the interface.

For explicit compile-time check, it is common to add an assignment of a nil pointer of the structure to a variable of the interface type:

var _ MyInterface = (*MyStruct)(nil)

This will force the compiler to verify that MyStruct implements MyInterface. If not, it will produce a compile error.

This approach is convenient for documentation and early error detection.