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.

Reflection with the `reflec | Go Programming Bootcamp lesson | BraveProgrammer
  1. Courses
  2. /
  3. Go Programming Bootcamp

Lesson 28 of 60

·Go

Reflection with the `reflect` Package

20 minutes

Course progress47%

Reflection Basics

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

reflect lets you inspect types at runtime – useful for generic libraries.

func Dump(v interface{}) {
    rv := reflect.ValueOf(v)
    rt := rv.Type()
    fmt.Printf(, rt, rt.Kind())
     i := ; i < rv.NumField(); i++ {
        f := rt.Field(i)
        val := rv.Field(i).Interface()
        fmt.Printf(, f.Name, val)
    }
}
"type: %s, kind: %s "
for
0
"%s = %v "

Setting values via reflection

func SetField(obj interface{}, name string, value interface{}) error {
    rv := reflect.ValueOf(obj).Elem()
    fv := rv.FieldByName(name)
    if !fv.IsValid() || !fv.CanSet() { return fmt.Errorf("cannot set %s", name) }
    fv.Set(reflect.ValueOf(value))
    return nil
}

Warning

Reflection bypasses compile‑time safety and is slower – use sparingly.
Make it stickPractice what you've learned1 quiz · 3 practical tests · Optional activities from Go Programming BootcampBrowse activities
Previous lessonGraceful Shutdown of ServersContinue to lesson 29Generics – Type Parameters (Go 1.18+)Back to Go Programming Bootcamp curriculum