Sobes.tech
Junior — Middle

How can you verify if a structure matches a given interface?

sobes.tech AI

Answer from AI

In Go, interface compliance is checked implicitly — if a struct implements all the methods of an interface, it satisfies that interface.

For explicit compile-time checking, a common method is to use a compile-time check via assignment:

type MyInterface interface {
    DoSomething()
}

type MyStruct struct {}

func (m MyStruct) DoSomething() {}

// Compile-time check
var _ MyInterface = (*MyStruct)(nil)

If MyStruct does not implement MyInterface, the compiler will produce an error. This is a convenient way to ensure that a struct conforms to an interface without running the program.