0Pricing
Go Academy · Lesson

reflect.Type and reflect.Value

TypeOf, ValueOf, Kind, and Elem

reflect.Type and reflect.Value is a free Go Academy lesson on CoddyKit — lesson 1 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.

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

Frequently asked questions

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

Yes — the full text of “reflect.Type and reflect.Value” 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 “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; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

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