WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Go · Intermediate · question 32 of 100

What is reflection in Go? How and when would you use it?

📕 Buy this interview preparation book: 100 Go questions & answers — PDF + EPUB for $5

Reflection in Go is the ability to inspect and interact with the properties of variables, data structures, and interfaces at runtime. The ‘reflect‘ package facilitates this by providing methods to examine the types and values of variables, as well as to update them, call functions, or perform other manipulations.

Using reflection can be convenient and powerful, allowing you to write more flexible and general code. However, it can also be slower, harder to understand, and more error-prone compared to other alternatives. Therefore, it should be used judiciously, and only when the benefits clearly outweigh the potential drawbacks.

To explain how reflection works in Go, let’s consider the following example:

package main

import (
    "fmt"
    "reflect"
)

type Developer struct {
    Name  string
    Age   int
    Skill string
}

func (d Developer) Code(language string) {
    fmt.Printf("%s is coding in %sn", d.Name, language)
}

func main() {
    alice := Developer{Name: "Alice", Age: 32, Skill: "Go"}
    reflectOnDeveloper(alice)
}

func reflectOnDeveloper(d interface{}) {
    t := reflect.TypeOf(d)
    v := reflect.ValueOf(d)

    fmt.Printf("Type: %sn", t)
    fmt.Printf("Kind: %sn", t.Kind())

    for i := 0; i < t.NumField(); i++ {
        field := t.Field(i)
        fmt.Printf("Field %d: Name: %s, Type: %s, Value: %vn", i, field.Name, field.Type, v.Field(i))
    }

    method := v.MethodByName("Code")
    method.Call([]reflect.Value{reflect.ValueOf("C++")})
}

In this example, we have a struct ‘Developer‘ with fields ‘Name‘, ‘Age‘, and ‘Skill‘. We also have a method ‘Code‘ that accepts a language parameter. With the help of reflection, we can examine the type and value of a ‘Developer‘ instance, iterate through its fields and call its method ‘Code‘.

Here’s a step-by-step explanation of how the reflection works in the ‘reflectOnDeveloper‘ function:

1. Import the `reflect` package.
2. Obtain the `reflect.Type` of the passed variable using `reflect.TypeOf`.
3. Obtain the `reflect.Value` of the passed variable using `reflect.ValueOf`.
4. Print the type and kind of the variable.
5. Iterate through the fields using `Type.NumField()` and `Type.Field(i)` to obtain field metadata, and `Value.Field(i)` to obtain field value.
6. Use reflection to call the `Code` method on the passed `Developer` instance with a parameter "C++".

In summary, reflection in Go allows you to work with types and values at runtime, which can lead to more flexible and general code. However, it can also be slower and harder to understand, so it should be used with caution. In general, you should use reflection when other alternatives are not feasible or would involve excessive code duplication or complexity.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Go interview — then scores it.
📞 Practice Go — free 15 min
📕 Buy this interview preparation book: 100 Go questions & answers — PDF + EPUB for $5

All 100 Go questions · All topics