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

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hi welcome to go on the run and in this next set of videos we're looking a go reflection now this was a subscriber requested topic someone sent and said hey can you cover go reflection so hi this was over a month ago or something and we were already doing like profiling or add profiling plan something like that so anyway we're finally here that I can cover go reflection so I figured the way to look at global reflection is sort of ease into it now if you already have some idea how it works then bear with me and be patient otherwise I'll take the approach that I was not too many people know about it or at least the people who would like to see it would like to see a different take on it and so I'll sort of slide into it very easily so I will start off with a very simple program that is sort of derive from the Gulen for tourist course that's on udemy no no that I've mentioned that there's a perfect time for me to also mention that statue and towards the end of this video and I'll talk about how you can either get a free coupon to the Gulen futurus course or a discounted coupon I'll cover that at the end all right so let's start off with or a simple program oh one more thing I have reorganized the directory a little bit basically I had all these sub directories for the different projects that we were working on and topics and so on but I had no idea which one I covered first and so on so I took some time went back and renumber them if you already pulled on the code from get repository that's fine you can just simply do a git pull I will bring down the update and merge it in or you can just clone it again all right so that's that that's housekeeping so we'll start off like I said we're gonna do several videos so this is part one and so let's jump in I want to start with something very simple so I'm not even gonna run this application because it's so simple so let me close this so you can have an idea so by the way if you've seen all this extra information in my editor basically what I've done is I've installed a plug-in called get lens and so get lens and so if you like you can install that plug-in - I use get a lot not only for this course but I also artwork and so it gives me a lot of information about what's going on and so on it might be a little bit annoying so we'll see but I've been using it for like a week or two now and so far I really like it so this program is really simple I have two types defined have one call ID and it's on the line type as a string and then I have a struct called person with only one member called name and name is a string also then I have a main function in which I'm printing out some values the things I want you to pick out and identify is that we're sort of doing a sample of different types of values so why am I doing this well if we're going to be done with reflection we need to ask ourselves what is reflection and before I get into that I figured that oh we should look at how some of the built-in function use reflection to print out different values so if you look at the signature of this function it says that the type of it is a is a very otic parameter of type interface and anti interface and so in go we know that out everything goes a statically typed language so everything must have a type and so the only way we can then pass a number of values to this print line function even unknown types that we created was if there was some way for go to hide the specific of the type information that we're passing to it but still be able to retain that and be able to retrieve it when it needs to and so the empty interface allows us to that it basically wraps our value in such a way that truth gets wrapped in an empty interface all of these value get wrapped in the interface but still within the print line function and a set of function they can still uncover and retrieve that information so they can print these properly so when we run this this printers as a string and so on and so on and so on now we're gonna get to a little bit more of that detail so I'm keeping it very abstract so let's go look at our second example function seven piece of code and see what's difference so in this file what I've done it is essentially the same thing but now I have introduced a function called print line which is my own print line function notice I'm using print line from the F empty package but now I have a print line function and it is my own print line function which looks like it has the same signature except that it doesn't quite this print line function is a very attic parameter on interfaces mine is a simple single parameter on the empty interface and the way in which I'm using it is to say for this value X which is of type interface is a variable of an empty interface I would like you to print out the type and the value that's in X so here's my question let's say I were to remove this and so we were calling my functions instead so I do that save it and reformat and so no I call my function with true a boolean value then I call it with 2010 and integer value we can imagine or we can imagine that that's those values gonna get wrapped into this interface when I get in here and I ask what is the type of X which should it print out should it print out that the type of X is an empty interface or should I print out the type of X is the type of the value that is currently stored so if you don't know the answer to that now we can run it and we can see so let's go here and so we'll go to our reflection code and we do in part one and we're doing exercise too and so go run me and as you can see it actually print out the value the type and the value that's stored in X and not the type of X because the type of X is just empty interface it was really printing out the type of X it's really all of these should just simply say empty interface but they don't they actually have the type that is stored in there so what is going on this is sort of weird because if I was to change this to be another function let's say let's write use another function let's valid print to for example print line to and I say X is of type int of course we know that I can't pass anything other than an in to this function but when I ask what is the type of int it tells me that it's type int okay so the empty interface is special in that when you ask for its type even if I pass nil into this function when we ask for its type we will not get an empty interface right so the empty interface is the variables of empty interface is special the when you ask for the type they don't tell you that their type is this which is their static type what it declared us whereas here when we asked was the type of X it says it's an int we would expect if things are consistent that when we ask for the type of X it should say it's an empty interface but not that's not what we get what we get is the type of the value that is stored in X and that's very different and I'll show you illustration that I use in the course let's jump to that now this is for illustration purposes only it is not actually this way but it just helps you to think about what it's going on so let's say I have a variable called foo and it's type is empty interface what does this really mean well we can imagine that somewhere in memory that some memory and set of bytes are going to be allocated to represent my variable foo but there's something else that's happening the static type or foo is actually an empty interface but if but remember I'm saying that our food is this special type of variable once you create a verb of an empty interface it has these two like hidden fields that you're not gonna be able to see it directly but you can get to and I'll talk about that in a minute and we'll see it in code also and so here's how I can prove this if I assigned the value 3.14 toufool what happened is the value field or a value property of foo gets a copy of that value that I was trying to assign it and the type field or a type property of foo gets updated with the type that represents that value that I just stored so since 3.14 is a float 64 the type field says it's for 64 and the value has a copy of that value now if I should know create a person literal and take the address of it and assign that to fool guess what happens no problem well the value when I take the address of a person literal that's a memory location with a certain type I've created a pointer and so the point of value which is just represent is a number that gets stored in the value field or property of the full interface variable and then the type property gets the name of that type that I will value a store there so now you can see that our food is actually changed again what it's saying it's type is but remember when we call that function we ask what is the type of food or it keeps changing but that's because of this when we ask for the type of fool with actually reaching in and looking at these values and type and print and that out instead of telling us with the true type of foods which should be an empty interface okay so that's why I said it'll interface value variables have this hidden these two hidden properties and we can kind of see that all if that is the case that has these hidden feels the value and type fields that the that's why when you assign different values to it that's how it remembers and know what was the last thing that would assign to it and so you can keep assigning different things okay so through the things that we're gonna cover any code in this part one of go reflection is type assertions and type of certian is a mechanism to review that dynamic type right so you saw just not at all the variable foo look like its type was changing but go is a statically typed language we don't really have dynamic type but it certainly looked like if the type of food was changing well no it wasn't food self-type remained to stay the same as an empty interface what was changing is the information that foo contain that's what was changing and that's what it's dynamic and then we'll see something called type switching which allows us to create like a switch statement that switches on the type of a value so we'll use an interface variable and depend on what is stored in there we can execute code in a switch like statement and so it's called type switch and switching on type essentially so let's revisit this idea of dynamic versus static type no let's take away the case when we have full and it did not have a value and this was focus on when we assign a value to fool so as you can see when we assign the value 3.1 for a floating point value to fool well at that time foo the internal hidden properties of foo had the value that represents a copy of what we assign it and a type field that says what type of value that we were stored and the same thing when we assigned to fool a pointer to a person it was updated that way so we can ask ourselves what is Foos type we've as it's already in code when we look at the example just on where we try to run some code that said what is the type of food thing that we pass in and another way of really answering that question is see what if I tried to create a variable goo and said it's equals the fool when we have a value of 3.14 stored in foo what is the type of go because can we create that variable goo then gue should be float 64 right and if we try to create a variable who H oh ho when we have a pointer to a person then we can say well then the type of who should not be pointer to a person well let's say we have the variable foo again and we see where would is its static type well the static type of foo is an empty interface that does not change that's what static means does not change right and go is a statically typed language we do not allowing go variables to change their type once we declared a variable foo it type will remain unchanged as an empty interface so where does the dynamic type comment well that's because we see that all when you you can assign to food different types of values and what appears to happen what appears to happen is that first type is changing and its peers that way because if there you ask what is the type of foo it looks like it's changing and so that's the dynamic aspect of it but really what's happening it's just we were really getting to look inside of foo and access that type feel to see the different values at different times so but other than that we really do not have dynamic type okay well again you can say that though an interface value is a dynamic type but I want you to see that the type of that variable is not really changing is the information about the values that it's storing that is changing so let's get back to some code now that we see that's how when we ask for the type of X where X is an interface variable what we're really doing is as if we went into X and say give me this information and then here when we ask for the value is as if we say in this so let's look an another piece of code and so this is our third example and in this example I haven't changed my main function what's forever it is still calling our built-in function but this time I want to demonstrate this idea of that hidden field that I mentioned and so what I'm using here is type assertion I'm saying I have an interface value and I want to know what type is in there and so I'm saying can you I'm using a special syntax which is the variable dot and then look like a function call I'm saying try and take out or access the value part of this interface variable as an ID remember ID is our type that we've defined above as a string so I can get two values from this now if I only try to get the one value my program is going to crash if an ID is not stored in this interface if an ID value is not stored in this interface but by asking for two values what I get is a boolean that tells me yes or no so if the boolean is true that means yes an ID is stored in that value and if it's fault is means an ID is not stored in that value so notice that we call our function with several different values so for this one I should expect to see this message that says X has type ID which I have defined and then the value of it and I should be able to print out the value okay and as you can see V is the value here because I'm asking for the value I'm asking is it the type that I've asked for because I'm asking for a very specific type and of course if okay is false it means then that oh it is not the ID type and it's some other type and we can print out that information right now we still ask and go to do the heavy lifting because we haven't jumped into reflection yet but we'll get in there and so we were pretending again that X has these two pieces of info and the way you get to them is by specifying the type you want out of this interface value variable and if you can get it it returns true with that value and if you can get it well then this is going to be empty and you get false so let's run this code and see so this is our turn example so we go to or example three directory there we go and let me clean up the screen here a little bit and then we do good on me and as you can see this is not the type you want but once we see that I defeat type X has type ID which is what I've defined and the value of it is this no that's what our string so it just printed out the string and then these are all the other things that were in there that we didn't want okay so that's one way in which you can use type assertion I'm asserting that all the value in X is ID and if I'm correct then print it out if I'm not correct then oh well I was wrong okay no let's look at type switching so in this example again main hasn't changed and so if you look at our function now still look the same except no I've changed from a type of switch know before I was simply doing something like this I was saying v colon equals 2x tack and I was asking for a specific type no I said you can do this if you're absolutely certain that X contain ID because if it doesn't your program will crash but if you don't want that and you say instead just tell me if I do have that value then you can use this and then if you're correct in your assertion then this will be true if you're incorrect and it happens so that it so happens that X does not have what you're asserting then uses a giimpse get a false and you could test that notice how this looks very similar to this X we're not asking for a specific type we're using a special type keyword and we're saying tell me the type that's in this interface verbal and I'll use that in a search statement now you might be tempted to think that all you can do this and then you can store that in some variable called T for example and so T would contains the type of X but no you can only this is only valid in the present of a switch statement which is called it types which is a special type of switch statement so here we go switch on the type look in this X find the type and notice it almost look like what I said earlier that X as a hidden feel called type except you know you don't really access it but if you think of it that way you know it's sort of help you understand what is going on and so give me the type of X that's currently an X by the way and because we know the type of X is empty interface that's not what I want to know though I want to know the type of the things that's being stored and hidden and wrapped inside of X and then if it's boolean then I can print out boolean or I can say whatever I want I can see this is a boolean value yeah you know something like that so whatever you want you know or if you want to handle those case right and so you get the idea and I can look for I can specify the type that I would like to retrieve from this thing so I can put keys statement for those once we get here it is a float so it's okay for us to ask for that float value and I do not need to ask for both because why I know it all I will not crash because I'm asking specifically for a full full value there's no doubt at this point you see before I did not know for sure if there was fluid so I had to be cautious and get both values but no I don't need to do that so I can just ask for the one value and that's what I'm doing here and then there's some unknown they're not really unknown is just because I didn't want to write typo'd all of these that's why and I'm saving it for later when we're gonna use reflection to get all this sort of information because as you can see you can easily get very repetitive like imagine we had a five element array a six elementary I have to write a kiss for each of one of those but with reflection we will see later on we'll be able to still determine and figure out if it's an Arri how many elements if it's a slice and all those that stuff the capacity and everything and if it's a struct where the fields names are and the type we're gonna be able to do all that work reflection okay and I still haven't mentioned what reflection is yet either but let's just go very slowly so let's clean up and run our code so this is example four and so go around main and you can see there's a boolean value it's true this is my nice int value and table and you can see I passed these two through and it printed out to come flex number on a string and because I wasn't testing for my ID type and the personalities are the thing and so so you get the idea as you can see that we're able to switch on what the type is but this again is not the best way to do things because you would haven't you need a case statement for all the power of all types I notice here we're naming the type that we've created above which is person how do does the goal language deal with our person when we pass it to the print function when it does not previously know about it so it could not have used something like a case switch statement because then it wouldn't be able to name over type it doesn't know all the other technical possibly exist so the last example is illustrated in that same problem that I just mentioned and that is to say if I were to remove my function my print function and put it into its own package which I did so I create a diet recall fool I put print that go in there no because I need to access my print function from that package food that's why I see food at print and this is the way I import it and to find the easiest way to put it let's just go to that directory 5 and my package foo and I can say go doc in this directory and notice this nice little trick I covered this in the goal line for tours course though you can see when you have a package what is the documentation on how to import it and so on and so it tells me this is how I import this package and so I can easily just cuddy this and go back to my code and paste it in green here ok so know here's the problem though I am pass in person and a pointer to a person and if I look at my code it's trying to use person but this is the package foo it does not know about main that person personally define it main so if we go to this dietary and we try to build this it should fail and we should expect that because it doesn't know who person is and so therefore this is not a good way for dealing with tags that we previously don't know about putting it using a case statement which is exactly what I mentioned so if we comment this out now and clean up and rebuild it pulls fine then I can go up to my coat my Mane that you hear where I just have to mean that go file and I say I can say go run mean and know it runs just fine but of course this is not handling our custom types of course but we will see how to address that with reflection so that is all I want to cover from now if you want to get a jump on some of the reading material here are some references or resources so I talked about interface types and you can look at this blog post on laws of reflection and also you can look at the reflect package but these are the things I will be talking about more in the other set of videos no I said that I have announcements to make I did the Golan with Taurus course and I put it on udemy and one of the reason I did that was to not only be able to have a sort of professional course out there available a course in which I went true took my time and tried to make it as good as I can of course I'm still learning but also to see if I can get financial support for what I'm doing this takes a lot of time and there are things I could be doing but I love it I really do love and enjoy doing it and if I have a choice this is what I would like to do however I understand that not everyone can afford to pay for the course with that in mind some of the viewers have been asking for either a lot of contests or coupons and so on so this is the announcement if you can afford to pay for the course please do help me out help out the channel by buying the course if you can't afford to pay for it if you are in a situation or position financial position where you can even donate to the channel I'll show you the links here you can donate by sending your donation to my paypal account if you have PayPal or if you into digital currencies you can do that also I can't think of any other way besides PayPal or digital currency so definitely let me know there's something called patreon they haven't gotten wrong to send that up yet so that might be something we might do later and if you cannot afford to pay for the course but you still like to pay for the course but you can't afford to do the folk among here is a coupon for about half off the course is on udemy at $25 here's a coupon for $13 off that's US dollars so it's gonna just in wherever you are so that's 50% off if you need it know if you're still not able to buy the course or pay 50% off for the course then please email me and let me know and I will give you a free coupon now the reason why I don't want to post a free coupon is because I don't want it to be abused I don't want to put up a free coupon for let's say maybe 50 users and then one person just get it and go you know users up and people who actually are part of the channel and this community don't get access to it so as much as I would like the financial benefit of people buying the course I also would like to know that all all the people who would like to see the course can see it if you send me an email for a free coupon like I said I will give you a free coupon no other requirements but just know that oh you might have to wait a little bit simply because I have a day job also so I'm not sitting there just looking at emails but just know that oh hey we'll send you if it gets overwhelming for some reason like I have 2,000 people sending me email it's not I wouldn't reply it's just that a second with some other way of getting those coupons out so in short I hope this is a way for everyone to be able to participate in Google and futurus I really like the course and I think it's perfect by any stretch of the imagination I look forward to doing another course and I hope that with this sort of flexibility that we'll be able to reach as many people as possible those who can't afford to do it have a way of getting it for free and those who can pay something can also do that and then those who can pay more can also do that by the binder course full price or making a donation with that I don't want to make this any longer if you have suggestion of how I can help other people have access to this course please let me know at the end of the day what I why I do this is because I really want to help and I love it I absolutely love consuming the information and then finding ways of sharing that with other people and I learn the same way by not only trying to share it but in discussing and talking and other people share it information that they have so have a great rest of day and see you in the next video take care bye [Music]
Info
Channel: Vilito Exquisitus
Views: 7,520
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: oiX7fAmOYX0
Channel Id: undefined
Length: 31min 22sec (1882 seconds)
Published: Mon Jan 21 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.