ProgramGuru

Polymorphism in Go

Start with Polymorphism in Go

Polymorphism lets one interface-based operation work with values of different concrete types, each supplying its own behavior.

What you’ll learn

  • Explain the main purpose of polymorphism in go.
  • Read and run the complete Go example in this lesson.
  • Apply the concept and recognize common mistakes.

Understand the idea

Go primarily achieves this kind of runtime polymorphism through interfaces. When a function receives an interface such as Shape, a method call dispatches to the implementation belonging to the concrete value.

There is no class inheritance requirement, and the function does not need a long conditional chain identifying each shape. Prefer behavior-focused interfaces when multiple concrete types should be used interchangeably.

Visualize how it works

Follow this diagram before executing the example. Check where a value or decision changes what runs next.

Visual explanation for polymorphismOne interface call dispatches to different implementations.Shape: Area()Step 1Square → 9Step 2Rectangle → 20Step 3showArea calls AreaStep 4One interface call dispatches to different implementations.
One interface call dispatches to different implementations.

Run a complete Go example

This example is a complete program. Save it as main.go or paste it into the Go Playground.

Go code

package main

import "fmt"

type Shape interface{ Area() int }
type Square struct{ Side int }
type Rectangle struct{ Width, Height int }

func (s Square) Area() int    { return s.Side * s.Side }
func (r Rectangle) Area() int { return r.Width * r.Height }
func showArea(s Shape)        { fmt.Println("Area:", s.Area()) }
func main() {
	showArea(Square{Side: 3})
	showArea(Rectangle{Width: 4, Height: 5})
}

Output

Area: 9
Area: 20

Trace what the program does

Square and Rectangle both implement Area, with different formulas. showArea invokes the method of each actual shape, resulting in 9 and 20.

Avoid common mistakes

  • An interface value does not copy all behavior into a new subclass.
  • Avoid downcasting merely to choose among implementations when the interface method already describes the behavior.

Try it yourself

Add a Triangle struct with Base and Height and an integer Area method, then call showArea.

Tip: Edit the example above, predict the output, and run the modified code to test your reasoning.

Check your understanding

What enables showArea to work with two shapes?
Choose one answer. You can try again.

Answer and explanation

Both satisfy Shape through Area()

Correct. Square and Rectangle both implement Area, with different formulas.

What you should remember

You have practiced Polymorphism in Go in a small, self-contained program. The important habit is to predict what runs, inspect the actual output and distinguish the language rule from the particular example.

Before continuing, make sure you can explain the program to someone who has not seen it, including the line that changes its result.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Composition vs Inheritance →