Go is a statically typed language. In most cases the type of a variable is known at compilation time.
One exception is interface type, especially empty interface interface{}
.
Empty interface is a dynamic type, similar to Object
in Java or C#.
At compilation time we can’t tell if the underlying value of interface type is an int
or a string
.
Package [reflect](<https://golang.org/pkg/reflect/>)
in standard library allows us to work with such dynamic values at runtime. We can:
Related language-level functionality for inspecting type of an interface value at runtime is a type switch and a type assertion.
https://codeeval.dev/gist/ef044402a5c91d757186df7753fa8649
Basics of reflections are:
interface{}
typereflect.ValueOf(v interface{})
to get reflect.Value
which represents information about the valuereflect.Value
to check the type of value, test if the value is nil
, set the valueReflection has several practical uses.