Reflection

Let’s learn about reflection in Go.

We'll cover the following

Reflection in Go

We begin with reflection, which is an advanced Go feature, not because it is an easy subject but because it helps us gain insights into how Go works with different data types, including interfaces, as well as understand the need for interfaces.

One might wonder how we can find out the names of the fields of a structure at execution time. In such cases, we need to use reflection. Apart from enabling us to print the fields and the values of a defined structure, reflection also allows us to explore and manipulate structures like the ones created from decoding JSON data, where the fields and their types are not known beforehand.

The two main questions that should be asked are the following:

  • Why was reflection included in Go?

  • When should we use reflection?

Get hands-on with 1400+ tech skills courses.