Junior — Middle
How should interfaces be declared in programming?
sobes.tech AI
Answer from AI
In Go, interfaces are declared using the type keyword and the interface keyword. An interface describes a set of methods that a type must implement to be considered satisfying that interface. Interfaces should be declared minimally, only with the methods that are truly needed to ensure flexibility and ease of testing.
Example of declaring an interface:
// The Reader interface describes types that can read data
type Reader interface {
Read(p []byte) (n int, err error)
}
It is important to note that in Go, interface implementation is implicit — a type only needs to have all the methods of the interface. This allows for easy creation of mock objects and writing more flexible code.