David in Canada

 

Tech, life, and everything in between.

Day 10 — Interfaces and Polymorphism in Go

Updated at # Tech # Go

Overview

Today I learned how to use interfaces in Go to design flexible and reusable code.

Interfaces allow different types to share common behavior without explicit inheritance. This enables polymorphism and decouples implementation from usage.


What I Built

A Grading System CLI that:


Key Concepts

Interface Definition

type Evaluator interface { Evaluate() string }

An interface defines behavior, not data.


Implicit Implementation

func (s Student) Evaluate() string
func (c Course) Evaluate() string

Any type that implements the required method automatically satisfies the interface.


Polymorphism with Interface Slice

evaluators := []Evaluator{}

Allows storing different types (Student, Course) together.


Stringer Interface

func (s Student) String() string

Improves output readability when printing.


Example Output

Student Alice -> B
Student Bob -> F
Course Math -> A


Key Takeaways


Reflection

This was the first step into designing systems instead of just writing functions.

Interfaces make code more flexible and closer to real-world backend development.


Next Steps