hi there serdar here for infoworld in this episode of smartgo i'm going to introduce the basics of
reflection in go in programming reflection is the ability for a program to examine its own elements
at runtime and to do things like report back what type something is languages like java and
python and c sharp offer reflection in various incarnations and so does go and go the main use
for reflection is when we use the empty interface type to accept any kind of object and we want to
extract details about the object at run time this is not just the same thing as checking the object
against one of a number of possible types and taking action based on what that is although you
can do that this includes the ability to do things like inspect destructs elements dynamically in go
we use the reflect package to work with reflection it provides us with a few different
methods we can use to get information about data types passed through an empty interface
so let's look at this code to get some examples of what we mean by this this code passes three kinds
of variables through the empty interface into the describe function one of these is a float
another is a string and a third is a struct that consists of a string and an integer the results
are then inspected in the describe function by way of methods that reflect provides for us to do that
there's three methods that we're going to focus on for the sake of this introduction value of type of
and kind value of gives us the value of the data that's passed through the empty interface typeof
gives us what type it is kind is a little trickier to understand it's basically the type of a type
so let's run this code to see an example to understand what's meant by this as you can see the
value type and kind for a float and a string are going to be pretty predictable but when we pass in
a structure this is where things get interesting the type that we get for this structure is
main.movie that's the name of the structure that we created that's our user created type
the kind however is struct see normally what happens is that if type and kind are the same
then that's a sign we're dealing with an instance of one of goes primitive types like an integer or
a string but if it isn't then we're dealing with a user defined type the type will be what's the name
of the user-defined type the kind will be what that type is based on in this case it's struct
now one thing we can do with this information is use it to perform closer inspection of the
components of the struct so we have in this code a test to determine if we are dealing with a struct
type and if it is we can use the properties of the value of result which is of the type
reflect dot value we can then use that to figure out how many fields we have a destruct we can
iterate over those fields and we can extract type value and kind information from each of
those fields in turn by the way as an aside i'm using these special formatting codes here
to print this information from each field to the console but we could just as easily have used the
value of and type of functions to do that now a couple of things are worth noting here first
the field and num field properties these are things that only are valid if we're dealing with a
struct if we tried to call these on anything other than a struct value the program would panic so we
have to put in this guard to make sure that we're not calling these on anything other than a struct
second thing to keep in mind reflection through the enti through an empty interface type can be
slow compared to operating directly on known types so you want to keep the work that you do
with reflection and the empty interface types away from your program's performance sensitive
areas you don't want to be doing stuff like this in a tight loop if you can help it finally
you're probably wondering how the introduction of generics and go which i've covered previously
changes the use of reflection that's something worth going into in a future video there's a
lot to unpack there but the short version is that each of these things both reflection and generics
have their places and reflection is still useful even in programs that already employ generics
so that's it for this video if you liked it please leave a comment below don't forget
to subscribe to the tech talk channel on youtube and for more smart go and
more dev with sturdar be sure to follow me on facebook youtube and infoworld.com
you