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

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
get thee and welcome back to go on the road and this is part three of go reflection and so we're going to wrap up how we can use reflection to intergate on print out information about a struct for example struct value and this is just an example once you figure out or know how to do this then being able to use reflection to interrogate a value that represents a function and call that function or even if you have a channel how you can take that value reflected and then send or receive from the channel it sort of all just naturally falls out from the way that all you would use it once you figure out how to use it like one complex data structure like something like a struct alright so if you haven't looked at part 1 and 2 yet I suggest that you go look at those videos first and then come back alright let's jump in and so we were able to use the reflect package to now create a function which we call print struct in fo now what printer struct info does is it takes a interface value and it reflects it and it can tell you what kind it is and specifically if it's a struct if that kind of thing the value is given it a struct then it knows that all that value was a VIN number of fields and so it rates over those fields and you can tell you the field name and type as we can run this really quickly to see to remind ourselves what do we get from it so neglected terminal and let's go back to part two and this was exercise five for example we can see that when we pass something like this floating-point value it's not a strong value but when I pass a struck it says I know the kind of it is struct and these are the feel of it and package and so on so we can get all that information and so we see it so we can sort of infer the field names and their types the next thing we want to be able to do though is to be able to take this up a notch and see if we can print out the value for the structure for each field and so in part three we'll do that so let's jump in to part three let's close this and we'll start off by looking at me so this will mean function and other does is it's called our print length function that we have implemented we back in part one and what that looks like is a function that uses reflection it's been updated to reuse reflection to see that depending on the kind of thing I can of course see what it is so if I have a boolean value I can see that it's boolean and this is the value and that's not very interesting but we're able to use our prints truck expanded function or what we know from writing our prints truck info function we've implemented this function call prints truck expanded and what it does is if I am going to print out information about each truck if the value I'm given is a struct then I can call this function to do the heavy lifting for me and so it's verifies that it is indeed working with destructive value and we do the same thing we're doing before we know at all if this is a struct as a number of fields that type has a number of field and so we can iterate over each field of the type and we can get type information so for example we can get the type name for each field but we also know that since it's a struct and we have the value well each value must also have each field for the value must have its own value so we iterate over the value that we're giving that we have reflected and for each one of those feel of the value we're able to get the value and print it out and so that now allow us to print something like this what we can do here is print this out so let's get rid of some of the noise you really don't really care about the other thing the interesting thing is that no we can figure out the package and print out that this is a stroke this is the field name and this is the value so let's copy example one call it example two in example two let me get rid of some of the noise we really don't care about this anymore we know how to do this the interesting thing is to be able to see how we can print out the struct value and deal with when we have key pointer to a struct because right now when we try to print out a pointer trash truck even though we're able to print out each truck we'll try to print out a pointer to a struct it says unknown type so we want to fix that so if we go back to our print length function of course we can get rid of all these other cases we don't really care about these and for this case for a channel what we will do instead is we'll say we're looking for when we have a pointer so when we have a reflection that is of type pointer pointer so if you have a reflection of type pointer well we know that in our example at least what we're dealing with is a structure but we cannot pass that to our print struck function because it is a pointer so we need to get the value that our pointer value is pointing to and when we do that is we say reflect that indirect and if you look at this indirect function it says indirect return the value that the points to and this is exactly what we want because if we given a pointer then we want a value that is pointed to and of course if it's no we get 0 and so on so we want indirect of V and so this is going to be or v2 so this is going to be when we're given a Poynter let's go back it's a little messy when we're given a pointer to a struct we'll test and see yes this is a pointer but because we know it's a pointer to a struct we want to get the value of that stroke now and so that's gonna be our v2 and this is what we're going to now expand bill let's make sure we're using the right thing in Maine for example two we want to make sure that we are using to print from example two okay so let's rebuild that and we do exercise example two again and notice this does not look correct at all so what happened so let's go back and see what's going on so it looks like we should focus here so when we get the the value it is a value to reflect that value we really want to print out to reflect that value what we want to print out is the value that's stored in our reflected value so for that what we have to do is call it that interface function and what this does is it returns an interface value so this is sort of like how we can go from let me to this this is only how we can take a value turn it into an interface reflect that interface value to get a reflected value and now we were taking that reflected value and turn it back into an interface so it's going between interface empty interface to reflected value and reflected value to empty interface and that is exactly what we want to do and to know when we go back to the interface know we can print that out because notice what our function takes it takes an empty interface so let's build again I know this is a little bit confusing but if you sort of just go through example and play with it it sort of makes sense and so now you can see we actually printed out that value that second value and as you can see we are reflecting that value the only problem is though well our function is correct and printing out that this is a main person strapped but of course what did we pass we pass a pointer so we need this to fix that and make sure it so it prints out a pointer first so let's fix that in example three so what I'll do is a copy example to go here and paste it and in example three you can get rid of this because that's an executable the first thing I want to do in example three is fixed by import path so this is from import three and another one I don't really care about anything else in main that's okay what I want to do is work on this so the way I'll fix this is instead of oven or print stroke function actually write the output I'll have it return a string so that gives us a little bit more control so for example I can say s is equals to this you and then now our new line so that allows me now when we have to print out a point of value that I put a pointer in front of it and so what this means is that my print struck function here should return the string you and so let's go to example three directory and see if go build and I believe I fixed my main already so that's good and so if I run this yep there we go star - tomato I'm printing out a pointer - you mean that person so I can take out this quote so wrong in single quote here if you want because I believe that let's compare our full print line with the built in one so let's do fmt that print line and let's see how this prints out its value and so let's build again go over build and it doesn't print out the package name HP uses ampersand instead so okay we can do that we can use on per side and I included the package name but you don't have to include package shipping okay so that is an example of how you can interrogate structures to get information away and you and before as you saw we were able to get the name of this truck feels your type here we were getting the name and we printing the value so you could use this to actually construct a structure and so you can go either way either starting with an existing structure and then pull it apart and see what's inside or just create a structure at runtime I don't think too many places where you want to do something like that maybe unless you're reading data from some source and then you want to you can infer the data structure and then create a structure for it but again those are more abstract and I don't have a really example why you'd want to do something like that but maybe there's some edge case I hope this sort of illustrate how to use reflection if you still have questions you still want to see more about reflection let me know otherwise I'll move on and talk about something in the next video I'll I have a few ideas that I'm batting around so I'll see what I settle on okay then take care see you bye [Music]
Info
Channel: Vilito Exquisitus
Views: 1,721
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: OVRdeGs9ZjQ
Channel Id: undefined
Length: 14min 24sec (864 seconds)
Published: Mon Feb 18 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.