Go Academy · Lesson

reflect.Type and reflect.Value

TypeOf, ValueOf, Kind, and Elem

Lesson 1 of 413 steps

reflect.Type and reflect.Value is a free Go Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, and your progress syncs across the web and the CoddyKit app. The Go Academy course includes 4 lessons in total.

reflect package overview

The reflect package allows Go programs to inspect types and values at runtime. It is the foundation of encoding libraries, ORMs, and dependency injection frameworks.

reflect.TypeOf

reflect.TypeOf(x) returns the reflect.Type of x's value:

t := reflect.TypeOf(42)
fmt.Println(t.Name())    // "int"
fmt.Println(t.Kind())    // "int"
fmt.Println(t.String())  // "int"

reflect.ValueOf

reflect.ValueOf(x) returns a reflect.Value holding x. You can read the value with Kind-specific methods:

v := reflect.ValueOf(42)
fmt.Println(v.Kind())    // "int"
fmt.Println(v.Int())     // 42

Type.Kind vs Type.Name

Kind is the underlying Go category (int, struct, ptr, slice, ...). Name is the declared type name ("MyStruct"). Interfaces have Kind == Interface but no Name.

Struct fields

Inspect struct fields via Type.NumField() and Type.Field(i) which returns a reflect.StructField:

t := reflect.TypeOf(User{})
for i := 0; i < t.NumField(); i++ {
    f := t.Field(i)
    fmt.Printf("Field: %s Type: %s Tag: %s\n", f.Name, f.Type, f.Tag.Get("json"))
}

Reading struct field values

Use Value.FieldByName or Value.Field(i) to read field values at runtime:

v := reflect.ValueOf(user)
nameField := v.FieldByName("Name")
fmt.Println(nameField.String()) // "Alice"

Setting struct field values

To set a field, the value must be addressable (passed as a pointer):

v := reflect.ValueOf(&user).Elem()
v.FieldByName("Name").SetString("Bob")

Pointer indirection

If a value is a pointer, call v.Elem() to get the pointed-to value. Check v.Kind() == reflect.Ptr first.

if v.Kind() == reflect.Ptr {
    v = v.Elem()
}

reflect.New

Create a new zero value of a type and get a pointer to it:

t := reflect.TypeOf(User{})
ptr := reflect.New(t) // *User
user := ptr.Interface().(*User)

Calling methods

Call a method by name on a reflected value:

v := reflect.ValueOf(user)
m := v.MethodByName("Greet")
results := m.Call(nil)
fmt.Println(results[0].String())

Performance

Reflection is 5-10× slower than direct code. Cache reflect.Type and reflect.Value lookups outside hot loops. Use code generation when performance is critical.

Quick Check

What is the difference between reflect.Type.Kind() and reflect.Type.Name()?

Recap: reflect.Type and reflect.Value

Key points:

  • reflect.TypeOf/ValueOf for runtime type inspection
  • Kind: underlying category; Name: declared name
  • Value.FieldByName + SetXxx for struct field manipulation
  • Pass pointer + call Elem() for settable values
Free to start

Learn Go with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

Is the “reflect.Type and reflect.Value” lesson free?

Yes — any 3 lessons of the Go Academy learning path, including “reflect.Type and reflect.Value”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Go Academy course includes 4 lessons in total.

What will I learn in “reflect.Type and reflect.Value”?

TypeOf, ValueOf, Kind, and Elem 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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

How long does the “reflect.Type and reflect.Value” 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. reflect.Type and reflect.Value
  2. Inspecting Struct Fields and Tags
  3. Dynamic Function Calls
  4. Practical Reflection: Serializers
← Back to Go Academy