0Pricing
Go Academy · Lesson

Generic Data Structures

Building a generic Stack and Set

Generic Data Structures is a free Go Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why generic data structures?

Before generics, Go developers used interface{} slices (losing type safety) or generated code per type. Generics enable type-safe, reusable containers.

Generic Stack

A type-safe stack using a generic type:

type Stack[T any] struct{ items []T }
func (s *Stack[T]) Push(v T)        { s.items = append(s.items, v) }
func (s *Stack[T]) Pop() (T, bool)  {
    if len(s.items)==0 { var z T; return z, false }
    n := len(s.items)-1; v := s.items[n]; s.items = s.items[:n]; return v, true
}
func (s *Stack[T]) Len() int { return len(s.items) }

Generic Queue

A FIFO queue backed by a slice:

type Queue[T any] struct{ items []T }
func (q *Queue[T]) Enqueue(v T)     { q.items = append(q.items, v) }
func (q *Queue[T]) Dequeue() (T, bool) {
    if len(q.items)==0 { var z T; return z, false }
    v := q.items[0]; q.items = q.items[1:]; return v, true
}

Generic Set

A set backed by a map — key type must be comparable:

type Set[T comparable] struct{ m map[T]struct{} }
func NewSet[T comparable]() *Set[T] { return &Set[T]{m: make(map[T]struct{})} }
func (s *Set[T]) Add(v T)          { s.m[v] = struct{}{} }
func (s *Set[T]) Has(v T) bool     { _, ok := s.m[v]; return ok }
func (s *Set[T]) Delete(v T)       { delete(s.m, v) }

Generic Map helper

A type-safe functional Map over slices:

func Map[T, U any](s []T, f func(T) U) []U {
    out := make([]U, len(s))
    for i, v := range s { out[i] = f(v) }
    return out
}

Generic Filter

Return a new slice containing only elements satisfying a predicate:

func Filter[T any](s []T, predicate func(T) bool) []T {
    var out []T
    for _, v := range s {
        if predicate(v) { out = append(out, v) }
    }
    return out
}

Generic Reduce

Fold a slice into a single value:

func Reduce[T, U any](s []T, init U, f func(U, T) U) U {
    acc := init
    for _, v := range s { acc = f(acc, v) }
    return acc
}

OrderedMap

An ordered map preserving insertion order — useful for JSON serialisation with stable key order:

type OrderedMap[K comparable, V any] struct {
    keys []K
    vals map[K]V
}

Generic linked list

A doubly-linked list with type-safe elements:

type Node[T any] struct{ Val T; Next, Prev *Node[T] }
type List[T any] struct{ head, tail *Node[T]; len int }

Limitations

Generic types cannot use type switches on T. Methods on generic types cannot introduce new type parameters. You cannot specialise a generic type for a specific T (no template specialisation).

Performance

Generic instantiation in Go is done via "GC shapes" — types with the same memory layout share one implementation. This avoids code bloat while maintaining performance close to concrete implementations.

When not to use generics

Avoid generics for simple cases where an interface suffices, or where you only have one or two concrete types — the added complexity outweighs the benefit.

Quick Check

Why does a generic Set require the comparable constraint on its type parameter?

Recap: Generic Data Structures

Key points:

  • Stack, Queue, Set — type-safe with generics
  • Functional helpers: Map, Filter, Reduce over []T
  • Set[T comparable] for map-backed sets
  • No method-level type parameters; no template specialisation

Frequently asked questions

Is the “Generic Data Structures” lesson free?

Yes — the full text of “Generic Data Structures” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.

What will I learn in “Generic Data Structures”?

Building a generic Stack and Set You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Go Academy?

No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Generic Data Structures” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Go Academy lesson?

Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Type Parameters Syntax
  2. Constraints: comparable and interfaces
  3. Generic Data Structures
  4. Generics in Practice: Pitfalls
← Back to Go Academy