Sobes.tech
Junior — Middle

Have you used reflection in your projects?

sobes.tech AI

Answer from AI

Reflection in Go allows a program to examine and modify its own structure at runtime. This is useful when working with data types dynamically, such as in serialization, deserialization, or writing generic functions.

Example of using reflection to get the type and value of a variable:

package main

import (
    "fmt"
    "reflect"
)

func main() {
    var x float64 = 3.4
    v := reflect.ValueOf(x)
    t := reflect.TypeOf(x)
    fmt.Println("Type:", t)
    fmt.Println("Value:", v)
}

Reflection is powerful, but it should be used with caution, as it can complicate code and reduce performance.