Back to Concepts Roadmap
🔴 Level 5 — Methods & Interfaces
Methods & Interfaces

Interfaces (Duck Typing)

Implicit interface implementation, interface values (itab + data pointer), and the `any` type.

Real-World Analogy (Mental Model)

A universal electrical wall socket. Any device that has the matching 3-prong plug gets electricity — the wall socket does not care if it is a lamp, laptop, or refrigerator.

Key Concepts & Rules To Remember

  • Implicit implementation: if a type defines all methods of an interface, it automatically implements it (no `implements` keyword!).
  • Represented under the hood as `(type descriptor itab, data pointer)`.
  • `any` (`interface{}`) represents any type in Go.
Step-by-Step Code

1. Understanding Interfaces (Duck Typing)

Implicit interface implementation, interface values (itab + data pointer), and the `any` type. In Go, interfaces (duck typing) is designed around clarity and high runtime efficiency.

example.goGo 1.24+
type Greeter interface {
    Greet() string
}

type Robot struct{}
func (r Robot) Greet() string { return "Beep Boop!" }

Common Beginner Pitfalls & Mistakes

Mistake: Misusing interfaces (duck typing) without understanding its memory or concurrency semantics.
✅ Correct Way: Always follow standard Go idioms and verify with tests.
Self Assessment

Knowledge Check

Verify your understanding with these interactive practice questions.

Quizzes

Quick checks for understanding

Multiple-choice with inline explanations—expand to see why.

What is the primary concept behind Interfaces (Duck Typing)?

Finished this lesson?

Mark it as complete to track your overall Go mastery.