#golang #reflection #striversity Go Language Reflection - Part 2

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome to go reflection part 2 and if you join us now please check out the first video part 1 because I am picking up sort of exactly where I left off in that video before we get started if my voice sound a little bit different well I'm a little bit under the winner so hopefully it's not too bad I think it's okay for me to cover the material heads up that's what's going on alright so let's jump in like I said I'm picking up from where we left after last time let's open up part 2 exercises so the first thing I want to start with is sort of where we left off close this up here a little bit and so to review what we said is that if you have a variable of type into empty interface is you can assign it different values so in this example on line 7 we're assigning the floating point value to our interface variable X and our empty interface variable X and we can pull out the type or that information is not lost even though this is an empty interface and we can confirm that by asking what type is that value in X and we'll see it should say that it's for 64 and we could also get the value and we can create a variable using this the value that stored our empty interface variable and goo will be not of type interface empty interface but rather a float 6to4 and have the exact same value and type of what we store here and we demonstrate that by also changing the value in our empty interference variable X and again short so we can create another variable with the correct type and so if I run this now if you don't have cool runner install well it's just a plugin that you couldn't add by clicking here and the nice thing about it is that I don't have to use the command line for a single file go application or if I run run I run code from other programming languages also so I'll just use that for today and click run you can see it opened up this little window output window here and we can see that the type of X is float64 and the value is 3.14 which is exactly what I stored in there when I created a variable gu it also had the correct type of fill 64 and the value of 3.14 because that is what it's reading of the variable X and again we can see that who is also created with the correct type and value okay so that is a quick review so the key takeaway from our part one was that when you have a variable that is of an empty interface you can assign different values to it because it seems that the variable has two hidden fields or properties and those fields would you McCollum the value to represent a value that what you're assigning a copy of the value assign it and it type then record what type of thing that you have assigned to it and so what we're seeing here when we're able to assign a floating point value to X and then later a strut value or a pointer to a struct value it's really what we're doing is updating those two hidden fields we're not actually changing the type of exits F X type still remains as an empty interview and so we're able to sort of prove that by doing a type assertion which says basically if I have an empty interface value what I can do I can ask it for a the value of a particular type I specified what type we want and so now we will change to looking at how to use reflection to get that information the drawback with using type assertions is that you have to guess or know or be pretty sure or what type of value is stored in that interface variable because if you are the empty interface variable to be specific because if you ask for something that's not in there you program can crash of course we also saw that there's a way for you to ask for a boolean value that says tell me if I'm correctly sincerely and so we can do that or we can use the type switch which is what we use for our simple print Lily function and that was in exercise 5 of Part 1 and what we saw is that if we do it type switch then we can have this switch these cases that represent the text that we want to check for unfortunately it does not work for types that we do not know because we will have to know every type that we want to check and if this is a function that we need to enter someone else who is going to use it with their type that we have never seen well this is not going to work so that was the drawback and that's a quick review so let's continue so let's come back to our a second example of part two and we start off with an empty interface variable X we assign it a value but if you look on this line instead of trying to use a type assertion instead what we're doing is we're asking the reflects package calling the type of function in the reflect package and giving it a word interface variable or empty interface variable and what it will do is return for us a value of the type reflect that type and what type is what reflect the type is it's just a struct that hides some details about how go represents a type and they have also attached some methods to it and those methods allow us to do some very nice things with whatever this type information is for whatever is stored in our variable and similarly there's also if reflect that value type and we can get the value that or a type that represents the value of our variable by calling the value of method and you know that's sort of a handful but basically if you think of these two functions type of a value of as being able to look into our empty interface variable and access essentially that hidden type feel that I mentioned but it's not really there we don't know but that's what we were thinking of it and this guy access the hidden value feel of our variable then that is what is happening it's going and pull back peel away and get to those in fields that I mentioned and we sort of returning some sort of representation for it okay so once we have that know we can say well what is the type of X and we don't have to ask the printf function to tell us the type by using percent capital T instead we have that information already in our variable T which represent the type of our value that was passed in so we can simply print that out and we have the value represented in V so we could print that out now and so we come down here and do the sort of same thing and so if we run this code it's gonna be exactly like we had before the exact same result and as you can see here X type is 64 float 64 and the value is the same and we got the type using reflection instead of type assertion which is what we were doing for right or asking the printf printf function to get it for us no we're getting that directly and notice we couldn't get whatever the type is also using reflection so let's continue because I don't want this video to be too long again so let's sort of pick up the pace so let's contribute a second turn example and what I've done here is created a pointer to a literal value and this is for its type is anonymous is a struct with and just a single field called name of type string and again I'll do reflect the type of that and the value of it now when we print this she said of course expect that the type should be something you know pointer to the struct and so on and the value then here I have a new string as the thing I'll be creating and so this is going to be a pointer to a string somewhere in memory and I reflect that and of course printer the type so again we should expect pointer to type now before I get into this code let's just run that and make sure we see what we expect and then I'll talk about the few lies that I've just uncommented and of course this is consistent with what we expect you know we have type pointer to the struct and pointer to string okay great so let's go back to the code and let's uncomment this and then talk about its function so I've stored the types for each of these different values in their own verb of t0 and t1 and if we ask the types what kind are they the kind is sort of think of the kind as a super type no I was trying to depict this with some graphics and then I realized that other graphics is probably gonna be a little bit more destruction and more complicated than what it actually is is not as complicated as it might for scenes and so I'll run this and then we look at the documentation for what a kind is so let's just run this if we look we'll see a toe the kind for both of our types so type 0 whose type is actually a pointers to a strop it's kind is a pointer so kind like I said is that super type if you want or the super categorization for types and so even though you might have something with different types those type might fall into the same kind and so here we see that how a pointer to a strop and a pointer to a string has seemed kind of things because they're both pointers let's say I did a 0 colon equals to u a5 element area of it and I said a1 colon equal is a 10 element array of Gynt right or even I see this is fruit 22 for example well I need to create a value so the literal value so that's right and so this is this is a 5 element array of int and this is a 10 element area of throat 22 so there's very different types but they're the kind they're both Aries okay compared to let's see if I were to do something like let's just do this yes yes if I did that and I made these into slices instead so here well we know though this is not quite going to work I have to make a slice but so if I did that if I did that we can see that these are our are different slices this is a slice of in this is a slice of thought 22 but even so they're both still slices and so that's what the kinda refer to is that is that higher categorized category to which they belong and so let's go take a look at the reflection documentation and so if we go to the reflect package documentation and we go to overview and then we look at tight kind you can see the kind that it says a kind represent the specific kind of type that each type represents so just remember that type is also a type so we have lower case type as in this and we have this type uppercase as a type that was the that's defined in this reflection package and that is what we get when we do type of causing the type of function it returns a value of this type and so it represents that and so the zero kind is not a valid kind so every value that goal can manipulate or use falls into one of these kind and you can see them in your screen right now this is all of them so regardless of what type of area just like I showed in that example even when I add a five element array or a ten element array of different other type elements they are still kind array similarly channels all channels are just going to be kind channel every function you have ever seen main phone should regard to how many parameters regard so how many things is returned it's kind is still going to be function and so you still get the picture right pointer again regardless of what the pointer is pointing to it kind is a pointer of course you can then say well okay have a pointer to end and now I need to treat you like in on tiny ant or on saying or in value but at first is kind pointer and so with that in mind let's look at another examples or for example and so seem like I'm beaten this to death but I just want to really drive it all and so this sort of hopefully makes it clear so we have a string value which we assign to X then we have this new type that we've created call ID and its underlying type is a string and so we create that and assign it to X now this is a very different type but if we ask what kind of thing those two things are they are both still straining even though they are very different types and so we'll see their types are different but their kinds will be the same so if I run this we'll see that we have a string and if this is its value and then we have main ID as a different type this is its value but notice that both of these have kind string and that makes sense because when we are let's say using reflection to use a value of type main ID if we notice how its kind is string that sort of help us in how we can manipulate that value even though we don't know what the exact type is because the kind of string well we can treat it like a string and so that is one of the reason why being able to know the kind really helps you in being able to peel away at the onion that might represent a very nested type okay so the last example is sort of using this idea but what I want to do is a function that prints out information about a structure which is all the fields and instead so let's look at the implementation of the function so let me close this to give us some more room so the first line of the function is straightforward just simply getting the type of this value but because this function one expects to print fields from a struct if the kind of whatever the values passed it is not a struct I want to print an error message and of course return because there's nothing else I can do in this function right so t-that kind not equals to reflect that struct remember effect that structures a constant that we saw defined as one of the possible kinds and so I returned so that's straightforward and that's easy now that we once we're at line 24 we'll definitely deal with some kind of struck we don't know the exact details on the stroke but it is a struct that's what we know for sure and so because it's drug it can have any number of fields you can add zero fields because you can have an empty strap Orang could have greater than zero and so we get the number of fields and so we say we have a struct of whatever the type that structure is right wins which will give the specific type based on the value so this is if you have to create a value of this truck what is the type you need to use and so we print that out and then we also print out on many fields i under strict and then for each field we get to read notice we can say t that num feel now this you must be careful when use reflection because when you ever type their number of methods that are attached to a type but not all them are valid always it depends on what the type is so notice that num feel returns is struct type feel count it panics if the kind is not if drop so if you were to store any other value here that's why we had to check and make sure though it's off to kind struct because if we try to call this or let's say integer value or floating point value these values don't have fields so makes sense that this call should panic and so a number of other ones are like that too there's if you ever reflect a certain type for a function for example that type could tell you how many input parameters the function takes so many output parameters but you cannot call you know to get the number of in a number of out on if struck because a stroke doesn't have in parameter and a parameter right so make sure that you just read the documentation and once you go through the documentation you start playing with this a little bit it is not as intimidating as it first seems but just start doing the simple examples like I did here first and then get your sea legs and those and once you can sort of understand what we've covered so far I guarantee you that you will be ready to tackle the rest of the documentation it's a little involved but it's still detailed and so all right so now that I know that have it struct and I could get the number field now I can ask about the information for a given field and notice what I'm asking for is a value of struct field and so this no returns more information about it feel because once we're dealing with a struct each one of those fields have a lot of things you can have the name the type whether it's anonymous whether it's it has tags associated with it and so that is exactly what we use know to see well I want to print out field whatever for this stroke its name and its type and so hopefully it is a sort of a very straightforward example in the part three we'll see how we can use this information to then print out any structure in the way that oh the good printer function does it and as you could basically see that we have everything we need to be able to do that we have the type of the structure and we have the field and we have the type alright so in main what I do is to test my function I pass a float value we should expect that to see that you know I cannot really process this but then I pass a value of this person Val instruct and a value of this proto strap now since our function is not take it into a pointer I could have chicken say oh if it's a pointer is that pointer to is struct and then it has struct and i can also still process it but that's something else we can do but in this example I want to keep it very simple and just deal with stroke values and so let's run this and if we expand we can see that yes we get the error this is not a struct and then for the first stroke we see it so it's main that person it has one field and that field has the name name and type string for a second stroke value it's mean that proto it has four fields and those fulfills our source destination size and magic and notice there types string string on saying int int so that just tells us that how we can use reflection to get it all these things so I like to end it here because our server is gonna be very long take care and see you in the next video have a great day bye thanks for your time [Music]
Info
Channel: Vilito Exquisitus
Views: 3,333
Rating: undefined out of 5
Keywords: verrol, golang, go programming language, learning, tutorial, vilito, vilito.exquisitus, striversity, using gorilla mux web socket in go (golang), web application programming in go (golang), web application programming in go (golfing), using sever sent event (SSE) in go (golang) web applications, using web sockets in go (golang) web applications
Id: 9I0MMN3N0QU
Channel Id: undefined
Length: 21min 49sec (1309 seconds)
Published: Sun Feb 03 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.