Introduction
An interface in Go is a type that specifies a set of method signatures. Unlike languages such as Java or C#, Go interfaces are satisfied implicitly: a concrete type does not declare that it implements an interface. If a type has all the methods an interface requires, it automatically satisfies that interface. This structural typing keeps packages decoupled, since the type that implements an interface never needs to know the interface exists.
Cricket analogy: Go's implicit interface satisfaction is like a player who never formally registers as an "all-rounder" but is automatically treated as one by selectors simply because they can both bat and bowl to the required standard.
Syntax
type Shape interface {
Area() float64
Perimeter() float64
}
type Rectangle struct {
Width, Height float64
}
func (r Rectangle) Area() float64 {
return r.Width * r.Height
}
func (r Rectangle) Perimeter() float64 {
return 2 * (r.Width + r.Height)
}Explanation
The Shape interface declares two methods, Area and Perimeter. Rectangle never mentions Shape anywhere in its declaration, yet because it defines both methods with matching signatures, a Rectangle value can be used anywhere a Shape is expected. This is the essence of structural typing. Go also has an empty interface, written as interface{} or the alias any, which has zero methods and is therefore satisfied by every type, making it useful for holding values of unknown type.
Cricket analogy: Rectangle satisfying Shape without mentioning it is like a bowler who's never labeled a "death-over specialist" by the team sheet, yet gets used in that role in every match simply because their skill set matches, just as any Shape is expected to.
Example
package main
import "fmt"
type Shape interface {
Area() float64
}
type Circle struct {
Radius float64
}
func (c Circle) Area() float64 {
return 3.14159 * c.Radius * c.Radius
}
func describe(s Shape) {
fmt.Printf("Area: %.2f\n", s.Area())
}
func main() {
c := Circle{Radius: 4}
describe(c)
}Output
Area: 50.27Key Takeaways
- Interfaces are satisfied implicitly; there is no 'implements' keyword in Go.
- A type satisfies an interface simply by defining all its required methods.
- The empty interface (interface{} or any) can hold a value of any type.
- Interfaces enable polymorphism and decoupling without inheritance.
Practice what you learned
1. How does a Go type indicate that it implements an interface?
2. What does the empty interface 'interface{}' (or 'any') represent?
3. In the example code, why can a Circle be passed to the describe(s Shape) function?
4. Which statement about Go interfaces is TRUE?
Was this page helpful?
You May Also Like
Struct and Interface Embedding in Go
Understand how Go uses struct and interface embedding to compose behavior instead of using inheritance.
Type Assertions and Type Switches in Go
Learn how to safely extract and branch on the concrete type stored inside a Go interface value.
Methods in Go
Learn how to attach methods to types in Go using value and pointer receivers.
Structs in Go
A composite type that groups related fields together, forming the basis for custom data modeling in Go.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics