Go Tutorial - Interfaces Part 3 (Reflection)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys welcome to the third video of this four-part golang series covering interfaces this video will focus on the topic of reflection which is a relatively advanced topic and go if you've not already done so i encourage you to watch the first two videos of this series covering the basics of interfaces empty interfaces and type assertions this video builds upon those concepts the last thing i'll say before we get started is please do subscribe and like this video if you find it to be helpful or if you learn something new i promise i'm not just saying this to to increase my subscriber counter likes uh the subscriptions and the likes are really the feedback i need to determine if the tutorial is providing true value after i post the video i monitor the performance of it which is measured in subscription and likes to see if the content and the format is something that the viewers actually like so you know obviously the more subscriptions and likes i get uh the more you know of a confirmation that is that okay i need to create content similar to this but you know obviously on different subjects so please as i said subscribe like if you like the video enough said about that let's get started in the last video we left off talking about empty interfaces and type assertion we saw that an empty interface is really just the most basic interface that you can create because it declares zero methods and every type and go has zero or more methods so they all satisfy that empty interface requirement we saw that this allows you to create functions which accept an interface like this one an empty interface and then perform a type of searching on that interface parameter to extract the underlying value of that interface you can then execute some portion of code accordingly so this here is just the type switch statement which i also discussed in the last video uh where we're extracting that value and then we are running the code for the case that matches the type and so these types of rectangle and circle you know we defined these up here in an earlier video and i'll run this code just to ensure that the code still works fine all right in this line here don't worry about this it's just added to create some separation uh when it printed out to the console here but as you can see everything works fine here if the case runs according to the type that was passed in uh and if none of these match then it will run the default case i also mentioned that the reason you would want to create a function that accepts an empty interface is in a case where you don't know the parameter type that's going to be passed in at a runtime so for example if this code was running on a server you may get some input into this function that you didn't know about when you were first writing this program you may have no idea whether the input is going to be a circle or a rectangle or an integer and so you can write this code to basically create you know flexibility in your program so that we can handle each one of these parameters accordingly now the idea of reflection just builds upon that as we said the type assertion allows you to extract the value the underlying value of the interface reflection is just another set of tools that you can use to find out information about that underlying type and value uh and i guess at its core reflection refers to a set of functions that you can use to extract data about a variable during runtime and these functions are part of the reflect package so the first thing i need to do in order to use reflection is actually import that package and so i'll do that up here now reflection is really built around three core concepts and those concepts are types kinds and values let's look at types first a type and reflection is pretty much exactly what it sounds like it defines the properties of a variable what it can hold and how you can interact with it with reflection you're able to query a type to find out more about those properties and one thing i want to show you guys is if we look at this function type of which is in the reflect package we look at the return type there we can see that the return type is type capital t y p e and that is an actual type lower case t y p e that is defined in the reflect package and that's key to know so when we're talking about type kind and values we're literally talking about these these types that are defined in the reflect packets and we can pass in parameters values into these functions and get back an actual type that is reflective of the properties of this parameter that's passed into it so what we're doing here is reflecting the underlying type of this interface and we're reflecting it into this variable and so now this is of type type which i know that's really confusing but this type has a method on it called name right and that's what we're doing here we're calling the name method on this my type and so i'll run this and let's see what we get back and so we can see here that when we call the name function on a type of reflect dot type what we get back is the underlying type of the parameter that was passed in to this type of function so name is a function that we call on reflect.type and kind is actually the same thing so here i'm now making a call to the kind function on a reflect dot type and i'll run this and we can see that here what we get is actually the underlying kind of the parameter so i want to make that distinction between type and decline so if i scroll up here when we first defined these types we have a rectangle is the type but the kind is a struct same thing for circles circle is the type be kind is a struct and so kind gives us access to that underlying kind of the reflect dot type and in some cases like we see here with an integer which is defined here an integer's underlying type and underlying kind is the same thing now had i defined some other type up here that said you know my end of type and well in this case the type would have been my ant and the kind would have been it now there are also methods that you can call on this reflect that type that will only be valid depending on the underlying type so for example if i type in format dot print line and i want to type or i want to print out uh the number of fields right so this is a method that really only makes sense if the underlying kind is of kind struct right because only structs have fields if i was to run this right now it would actually panic because it will try to run it for this which is an integer and that doesn't have a field now i'll show you guys just so you can see what that looks like we can see here that it actually panics but if i comment that out now it's completely fine it's saying that this rectangle has three fields which we know is true down here and it says the circle has one field there are also some functions that only make sense to use if you are actually passing in a pointer so i'll illustrate that here if i change the parameters to these functions to all be pointers and get rid of this num fill actually what i can just do is type in name and i run that so lmelem returns the reflect.type for the type the pointer points to and so i know that how this can start to get really confusing and truthfully it's impossible for me to really go into all of the details of reflection uh because there are a lot of functions in the reflect package but hopefully what you're seeing here is that again the functions of the reflect package allow you to basically extract specific properties and information that pertain to you know a certain variable that your that you want to examine reflection is really not something that i find myself using often but if you use it you definitely want to make sure that you actually understand it because it can cause a lot of issues with your code and it can be very difficult for other developers to read it if they're not experienced with using reflection we see that you can print out the number of fields if it's a struct but you can even take that a step further what i've done now is actually call this function field on the reflect.type and i passed in an index so this index is actually referring to the zero base index of the field so when i define these types up here you've got these fields where width would be zero height would be one sides would be two and then circle only has one field where radius would be the zero index so what i'm saying is okay i want to call this field function which puts the field relevant information or properties into this variable here and then i can call uh field info dot name which is this is not a function call this is literally just referring to a specific property and then if i run this here you'll see that it actually provides me with the name of the field right and so again there's a ton of things that you can do if i just hit backspace you can see here all the different things all the different options properties that it tells me i have access to the tag the index the name the type so these are all things i encourage you guys to work with if you plan on using this package to at least help you get familiar uh before you implement it in any actual production code now the last core concept that i said reflection was based around was values and so if i change this type of here to to be value of and then i can just print out my type i'm actually going to get rid of all of this stuff here and when i run that you can see it actually prints out the same value that we get when we use this template string and the reason for that is because these functions within the format package deep down they're just calling some of these reflection functions and so again i encourage you guys to take a look at that because you can really start to get really deep down into this stuff but the primary thing to remember is that when you pass in something as an interface it always has you know underlying types and values if i pass in this rectangle as an interface it's got an underlying type and value and it's got you know properties and fields and we can reveal properties about this underlying type by calling these functions of the reflection packets we can we can find out how many fields does it have what's the value of these fields what's the type of these fields and so that's really the purpose of reflection you want to find out additional information about the underlying type and value of your variable so i think what i've described in this video is enough to get you started with working with reflection there's much more to it that you can that you can do with it but uh it doesn't really get any more complicated than what i've told you there are some functions that allow you to check if the value is kneel there are some reflection function functions that would allow you to actually change the existing value of a variable but if you guys want to learn more about it i'm going to actually advise that you read a book it's called learning go by o'reilly media and this is probably where i've learned a lot of the the core concepts about goal and language but it will it will take you to that next step in terms of working not only with with reflection but some of the other concepts we've talked about interfaces as well in the next video what i'll do is provide more practical examples covering all of the things that we've talked about with interfaces so i will give you more concrete examples on when and why you would use interfaces in actual production code so if you guys have any questions please put them in the comments again please do subscribe like this video if you guys have learned something new and i will see you guys in the next video the fourth video in this series covering interfaces on esoteric tech thanks and i'll see you later bye
Info
Channel: Esoteric Tech
Views: 1,210
Rating: undefined out of 5
Keywords: empty interface, go, go interface tutorial, go programming language, go programming tutorial, go type assertion, golang, golang interface, golang interface tutorial, golang tutorial, interface, interface golang, interface programming, interfaces, programming, tutorial, go reflection, go reflection example, go programming language tutorial
Id: Kqt6EI4ypsk
Channel Id: undefined
Length: 14min 42sec (882 seconds)
Published: Mon Mar 01 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.