Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Generics – Type Parameters | Go Programming Bootcamp lesson | BraveProgrammer
  1. Courses
  2. /
  3. Go Programming Bootcamp

Lesson 29 of 60

·Go

Generics – Type Parameters (Go 1.18+)

25 minutes

Course progress48%

Introducing Generics

This lesson explains the main concept clearly with beginner-friendly examples and background so you can learn why the code works.

Generics let you write reusable data structures without sacrificing type safety.

// A generic slice‑type that can hold any ordered type
func Index[T comparable]  {
     i, v :=  slice {
         v == value {  i }
    }
     
}
(slice []T, value T)
int
for
range
if
return
return
-1

Constraints

type Number interface{ ~int | ~int64 | ~float64 }

func Sum[T Number](vals []T) T {
    var total T
    for _, v := range vals { total += v }
    return total
}

Using generic types in structs

type List[T any] struct { items []T }

func (l *List[T]) Add(v T) { l.items = append(l.items, v) }

Info

Generics are compiled away – no runtime cost vs. interface{} implementations.
Make it stickPractice what you've learned1 quiz · 3 practical tests · Optional activities from Go Programming BootcampBrowse activities
Previous lessonReflection with the `reflect` PackageContinue to lesson 30Tooling – `go fmt`, `go vet`, `golint`, and `staticcheck`Back to Go Programming Bootcamp curriculum