Interface guards in Go

Open link in next tab

Interface guards in Go

https://rednafi.com/go/interface_guards/

I love Go’s implicit interfaces. While convenient, they can also introduce subtle bugs unless you’re careful. Types expected to conform to certain interfaces can fluidly add or remove methods. The compiler will only complain if an identifier anticipates an interface, but is passed a type that doesn’t implement that interface. This can be problematic if you need to export types that are required to implement specific interfaces as part of their API contract.

Interface guards in Go
Sign in to add comment

Turns out this technique has a name. I've seen them in terraform provider templates and thought "nice idea".