Junior — Middle
Kuidas tuleks programmeerimises liideseid deklareerida?
sobes.tech AI
Vastus AI-lt
In Go, interfaces are declared using the keywords type and interface. The 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 necessary 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, the implementation of an interface is not explicit — it is enough for a type to have all the methods of the interface. This makes it easy to create mock objects and write more flexible code.