Interfaces
Interfaces
In Go, an interface is a collection of method signatures that a type can implement.
An interface specifies a set of behaviors or actions that a type can perform, without specifying how those behaviors are implemented.
This allows different types to implement the same interface in their own way.
A type that implements an interface is said to “satisfy” the interface, and it must implement all the methods defined in the interface.
Where to use
- Abstraction - allow you to define a contract between different parts of a program without specifying how that contract will be implemented.
- Polymorphism - allows different types to be used interchangeably, as long as they implement the same interface.
- Testability - By defining interfaces for dependencies, you can create mock implementations of those dependencies that can be used for testing.
- Flexibility - makes it easier to add new types to your program and make changes without having to modify existing code.
Example :
package main
import "fmt"
type Shape interface {
Area() float64
}
type Circle struct {
Radius float64
}
type Reactangle struct {
legth float64
Width float64
}
func (c Circle) Area() float64 {
return c.Radius * 3.14
}
func (r Reactangle) Area() float64 {
return r.legth * r.Width
}
func printArea(s Shape) {
fmt.Printf("Area of shape: %f\n", s.Area())
}
func main() {
c := &Circle{
Radius: 5,
}
r := &Reactangle{
legth: 2,
Width: 3,
}
printArea(c)
printArea(r)
}
Use cases
- Standard library: Go’s standard library makes heavy use of interfaces. For example, the
iopackage defines several interfaces for input and output, such asio.Reader,io.Writer, andio.Closer. These interfaces allow different types of input and output streams to be used interchangeably, as long as they implement the required methods. - Web development: In web development, interfaces are often used to define the behavior of HTTP handlers. For example, the
http.Handlerinterface defines a single methodServeHTTP, which takes anhttp.ResponseWriterand anhttp.Requestand returns nothing. Any type that implements this method can be used as an HTTP handler.
Go interface vs java interfaces
Go interfaces and Java interfaces are similar in that they both allow you to define a set of methods that a type must implement in order to satisfy the interface. However, there are some key differences between the two:
- Go interfaces are implicit, while Java interfaces are explicit: In Go, a type satisfies an interface simply by implementing the required methods, regardless of whether the type explicitly declares that it implements the interface. In Java, on the other hand, a type must explicitly declare that it implements an interface using the
implementskeyword. - Go interfaces can contain any number of methods, while Java interfaces can contain only a fixed number of methods: In Go, an interface can contain any number of methods, including zero. In Java, an interface must contain at least one method, and it can contain any number of methods up to a maximum of 64.
- Go interfaces can be used as fields, while Java interfaces cannot: In Go, an interface can be used as a field in a struct or as an element in an array. In Java, an interface cannot be used as a field in a class.
- Go interfaces do not have static or default methods, while Java interfaces do: In Java, interfaces can have static and default methods, which are methods that have an implementation and can be called directly on the interface. In Go, interfaces do not have static or default methods, and all methods in an interface must be implemented by any type that satisfies the interface.
Interfaces are a powerful feature of Go that allow you to write flexible and modular code that can work with multiple types. They are used extensively in the standard library and are an essential part of the Go programming language.