Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does golang implement reflection?

Tags:

reflection

go

Using reflection, we can get the type name, storage size and the function of the given type(such as uint64, user-defined struct and so on). Even, we can modify some fields of the given type. How does golang implement reflections? I guess the following ways:

  1. Every type in golang, including user-defined type, itself has the information about type name, fields name and the function name. Golang reflection just reads these information or call the function.

  2. Through some mechanism, Golang can get the type name, storage size and so on. And the type itself doesn’t have these information.

I have read the golang reflection code roughly. I guessed that golang used the second way. Who can describe the concrete implement of reflection? Or recommend me some documents? Reading all code is difficult for me.

like image 477
Jianglong Chen Avatar asked Jan 05 '16 06:01

Jianglong Chen


People also ask

Does Golang have reflection?

Reflection is the ability of a program to introspect and analyze its structure during run-time. In Go language, reflection is primarily carried out with types. The reflect package offers all the required APIs/Methods for this purpose.

Why we use reflect in Golang?

Reflection in Go is a form of metaprogramming. Reflection allows us to examine types at runtime. It also provides the ability to examine, modify, and create variables, functions, and structs at runtime. The Go reflect package gives you features to inspect and manipulate an object at runtime.

Is reflection slow in Golang?

Reflection in Go is slow because it needs to do quite a few allocations.

What is reflect value Golang?

The reflect. ValueOf() Function in Golang is used to get the new Value initialized to the concrete value stored in the interface i. To access this function, one needs to imports the reflect package in the program.


2 Answers

This is just an overview and it might be not 100% accurate but hopefully you will find it helpful.

At build time Go linker will embed information about all types used by the application into the executable (https://github.com/golang/go/blob/master/src/runtime/symtab.go#L39)

Each interface value contains a pointer to the data type descriptor (https://github.com/golang/go/blob/master/src/runtime/type.go#L14)

During conversion from a type that is known at compile time to an interface value Go compiler will point type descriptor of this interface value to the concrete type descriptor (it is known at compile time!).

E.g. when you call reflect.TypeOf(uint(87)):

  • an interface value is created by the compiler that references uint type descriptor
  • this interface value is passed to reflect.TypeOf function as argument
  • reflect.TypeOf function uses type descriptor that has been stored by the linker in the executable to get the align (and other) information about uint type.
like image 100
kostya Avatar answered Oct 19 '22 11:10

kostya


The description of interfaces is well described here: The Laws of Reflection.

A variable of interface type stores a pair: the concrete value assigned to the variable, and that value's type descriptor.

Basically, type are known statically from your code. More flexible interface types keep the original underlying type for getting back to the original data type.

like image 23
Mickaël Rémond Avatar answered Oct 19 '22 11:10

Mickaël Rémond