Interface — contract of method signatures defining behavior. Struct — collection of named, typed fields defining data.
Interfaces in Go are implicitly satisfied — no implements keyword. Any type with the right methods satisfies the interface. This enables polymorphism, dependency injection, and clean testing.
1type Repository interface {2 FindByID(id int) (*User, error)3 Save(user *User) error4}56type PostgresRepo struct{ db *sql.DB }7func (r *PostgresRepo) FindByID(id int) (*User, error) {8 // database query9}10func (r *PostgresRepo) Save(user *User) error {11 // insert/update12}
Structs are the concrete types that implement interfaces. They hold data, support embedding for composition, and can have methods with either receiver type.
1type User struct {2 ID int3 Name string4}56type UserRepo struct {7 users map[int]*User // embeds storage8}
When to use:
Common mistakes:
any — loses type safety.