Interface — defines a set of method signatures (behavior). Struct — defines named fields (data).
Interfaces in Go are satisfied implicitly — any type that implements all the methods automatically satisfies the interface, no implements keyword needed. This decouples consumers from concrete types and enables polymorphism.
1type Writer interface {2 Write([]byte) (int, error)3}45// *os.File satisfies Writer without declaring it6func save(w Writer, data []byte) {7 w.Write(data)8}
Structs hold the actual data. You attach methods to structs (pointer or value receiver) to define behavior. A struct can implement multiple interfaces simultaneously.
1type File struct {2 name string3 size int4}56func (f *File) Write(p []byte) (int, error) {7 // implementation8 return len(p), nil9}1011// File now satisfies both Writer and fmt.Stringer12func (f *File) String() string { return f.name }
When to use:
Common mistakes:
interface{} (now any) excessively instead of defining proper typed interfaces.