Understanding INTERFACES in GOLANG

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so if you feel like this course just sort of like shifted gears and like you know went from fourth gear to fifth gear and it's like accelerating there's some truth to that and you know we're dropping into a little bit you know more intense material and we're driving for it a little bit more quickly but don't let that discourage you like just kind of settle back in your seat and enjoy the ride and then make sure that you practice this stuff and and drill yourself do exercises I'm gonna give you hands-on exercises also but drill yourself and work something until you've got it so create you know create some of some strux some user-defined types and attach some functions to it create some methods and then use those call you call those and then when you've got that okay cool go on to the next one but it's like anything like any sport like riding a bike you know or playing basketball it's like practice that layup practice that layup practice that layup and then pretty soon you own that layup and that's something that you have in your toolkit that you can use in the game how do you like that analogy alright so now we're gonna learn about interfaces and interfaces allow us to do polymorphism and and so to create an interface I'm going to you know the classic way to talk about this is interfaces allow us to define behavior and that's kind of like once you know what interfaces are that makes sense but until you know what interfaces are it's kind of like Oh what does that mean so interfaces allow us to define behavior and then also allow us to create to do polymorphism so let me let me show you what an interface is so we're gonna use the same syntax here type and then some identifiers and then like something else right there like you know some some other thing we're gonna do type human interface right and then we're gonna define it and we're gonna say that any buddy who has any type that has this method speak is also of type human so notice just that similarity there right where we do type secret-agent struct type human interface you know we're creating our own type or our X int you know it's like here is is that the keyword where is the top of the language specification where is my scrollbar there it is I am way down here keywords so we have var we have type right so we have our and we have tight so we use the keyword and then some identifiers and then here we have some other type so keyword identify or tight here the keyword is tight so keyword identify or type just notice that pattern because some people get confused like oh how do I declare a variable well keyword keyword identifier type oh how do I create my own user-defined type well keyword identifier type right and or whatever it is type food int other lying type is an int how do I do an interface keyword identifier and then type interface okay and if we go and we look at this right interface is a type interface is a type noticing the pattern so any other type that has the math that speak is also of type human you a value can be of more than one type a value can be of more than one type a value can be say it with me of more than one type a value can be of more than one type so down here we have a value and a value right here that value is of type secret-agent right so here's the value it's of type secret-agent and it's been assigned to the identifier the variable s a1 so this value is of type secret agent but because it also has the methods speak attached to it it is also type human so this value is type secret agent and type human so value can be of more than one type a value can be of more than one type here you know like you know just so we're clear on values x : equal 42 right this value 42 is going to be of type int right so values of a certain type this value here is of type secret agent and type human a value can be of more than one type and so we could do things like funk bar takes in a human and then does something okay that's it I called human pretty cool and we'll start with that statement right there and so now we could create a person and we'll create another person right here and we'll do p1 : equal let's see so we have speak and we have speak yeah we'll do p1 and this is gonna be a person and so here's our composite literal and then we need first and that's gonna be dr. and we need last and we need no so that's dr. know we'll mix it up and this next James Bond movie that we're creating or Miss Moneypenny becomes the hero there's gonna be a doctor yes I don't know what that looks like it's more positive though and now we'll print out dr. dr. yes do you want printing out dr. yes format run all right so there's dr. yes cool and we can see that these are calling speak and and then we'll create another function and this just gets you know you'll you'll get into this as you get into the language so I'm just introducing you to it and now we're gonna attach to type person speak right and we'll do the person speak that there do that they're cool and so now type person has the method speak attached to a type secret agent as a method speak attached to it so they both have the method speak attached to it and any type that as the method speak is also type human and then bar takes a human and and and can and I called human bar takes human and you could pass a human into bar that's what we want to show I was passed in the bar passed into bar there would caught that and it'll print out the age cool so now each of these we can do bar I think pass in sa one pass in sa to and we could pass in p1 so notice what happened bar is taking a secret agent taking a secret agent this is a value of type secret agent this is a value of type person so bar is taking in a secret agent and a person and and it's able to do that because both secret agent and person both secret agent and person are also type human and we've asked bar to take him type human right so that's taking in type human and that's polymorphism poly mini morph change right so this function can take in many different types right and depending upon those types we could change what that function does if we wanted to and polymorphism so that's a that's interfaces and interfaces allow a value to be of more than one type and both human both the person and secret agent both have a method speak so they are also any value of type person or any value of type secret agent they're also of type human and so then we could pass around a human and ask for a human this is really useful because if we go to the dock and we look at like IO writer right and we look at type writer and I'm just looking at type writer which is a funny type right here type writer is an interface and any type that has this method right implements the writer interface and so we could create functionality like we go to go dot org and look at net HTTP we could look at the index here and we can see what takes a writer yeah we could see the writers that are taken I'm just looking for like all these things are taking a response writer right and a response writer here's here takes an IO writer the header takes an IO writer at HTTP here takes an IO writer right so we could write things and so writer allows you to pass anything in and that implements the writer interface so you could have different elements which work where if they have that method and they could all implement that method slightly different differently so it's a it's a big thing to wrap your head around and this is just a start if you're just being introduced to it but if you are familiar with it then you're seeing how it works so let's watch this run undefined s and ass first s s first line 22 P all right and I was passed into bar James Bond true I was passed into bar Miss Moneypenny true I was passed in a bar doctor yes so that's pretty cool and that's polymorphism and one more thing I'll say about that maybe two more things is is that here's my joke an interface says hey baby if you've got this method then you're my type hey baby if you've got this method if you have this method or these methods then you're my type if you have these methods and you're my type that's what an interface says if you have this method then you are also my type and person has that method so person is also that tight and secret agent has that method so secret agent is also that that type so that's that's what a an air face says and then we've seen conversion so I'll show you conversion and so just give you an example of conversion and conversion is if we have bar you just think this through come up here and we'll do my own type so the keyword identifier and I need something here I don't know hot dog and the underlying type is an int and then if I do bar X hot dog equals 42 format dot print line text format print F % T backslash in X right and then format dot print and then Y bar Y int and Y is equal to int conversion around hot dog right and then we could do all that with y so we have 42 we have hotdogs right and then we have 42 and we have int because here we did conversion that's conversion so now I want to show you assertion so assertion is you're asserting something is of a certain type and so now we could do this and this is just FYI kind of like a nice tour it's showing you what's possible and giving you a look at the more intermediate to advanced material so I can't just say H first because this might be secret agent or it might also be a person and so I could do H that's fine it'll dump everything out that totally runs so we're back to running but what I could do is I could come up here and I could do a switch statement so remember we have switch and then we're gonna have something there and if I just leave a blank it'll switch on you know expressions that evaluate to true or false being the first one that's true and then we'll have cases I'm gonna do switch H and then I'm gonna do type this is a special type of switch statement where I could switch on type and then I'm gonna say hey if you're you know if the case is type person do something if the case is type secret-agent if that's what the type is do something and I have some code here which you could just kind of pause your video and you can see this and gives you a little bit of insight into that including the empty interface and I'll talk about that just one second but again this is just a preview deal and so now I could say what I want it to do for each of these cases and I could use a certian here so I'm gonna assert and this is different than conversion I'm asserting that this is type person and I'm asserting that this is type secret-agent and I know that because I just did that switch and now I could call first right I could call first off of that because I've said hey you know what I know this is type person we just checked it so let's uh let's call first off of type person so when you have an interface you need to get back to the concrete type the type underlying that interface you could do it this way format it run it and now you can see and let me just change this so we could where that is happening format it and run it and you can see here right that that was that one worked I called you know James I called Miss and I called dr. dr. and so that's assertion so conversion was down here that's conversion and assertion as I am asserting and that's a little bit stronger converting I'm converting that that value to a different type right and that makes sense I converted type hot dog or is it converted type hot dog to an int conversion Here I am asserting alright that's kind of like a strong active word I'm asserting that this is tight person I'm asserting that and I checked it with this switch statement on type so that's that's how you can kind of like once you have something that's general you can get back to the specific or the underlying concrete type seems like there's one more thing I was gonna say about that I'll remember what it was over you watch this video oh yeah that's what I wanted to say so that's a that's a whole lot about interfaces and polymorphism and don't don't feel like you got a nail that if it's new to you but if it's something you're familiar with then you know it's good to see and good to know about and and as you program you'll learn you'll learn more about it so let me just see if anything else I wanted to say comes back to me I guess the last thing I'll share is just a little bit of a review and remember what interfaces do so interfaces allow us to interface allows us to interface in a different way and so a value can be of more than one type and so an interface says if you have these methods then you're also my type and remember this interface down here type human interface right this has its saying if you have this method speak you are also of type human so both person and secret-agent have the methods speak so they are also of type human and that's a common type shared between them so when we ask for a value of type human like we do here right either a person or a secret agent could be passed into that and and that allows polymorphism so that's kind of what interfaces are and if you have an empty interface right so if that was an empty interface then every other type is gonna have no methods at least they also have no methods and so everything implements the type empty interface so when you see and go programming something asking for the empty interface and that's the other thing I want to say when you see in go programming something after actually asking for the empty interface every value can be put in there and we see that in the documentation so if we go and we take a look at like you know go dot org and we'll just look at the funked package right and inside the func right here these are all asking for an unlimited number of types here's print line unlimited number of values of type empty interface every value implements type empty interface and so every value could be put there or no values zero or more so now you understand this thing a very attic parameter of empty interface you understand that every type has no methods they might also have one or two methods but in addition they have they don't have any and so that's that's a they implement the empty interface and they could be you know every value is also a bat type so even an INT is also of type empty interface and so values can be of more than one type that's a profound concept and provides a lot of power and flexibility in creating programs so good for you for hanging out for this entire video because the last thing I'm going to introduce you to is Bill Kennedy has this article on composition you can just search for Bill Kennedy composition composition with go going go programming we've been talking about composition and here he lays out a program which has nice interface design right so creating a struct and then interface right and another interface and another interface and he shows you how you could start to build programs using interfaces so this is a nice nice nice document nice article to read through and don't Kennedy's a personal friend of mine so I feel like I feel really fortunate and lucky both bill and Caleb dachsie like they're friends of mine and and it's nice I don't know it's nice nice to share the good work that they're both doing in the world helping people learn about programming to learn about this language so they're great resources to consult with alright so that's a fair amount about interfaces that's a fair amount about interfaces and polymorphism and I hope you know watch this video again if the one a little bit like the concepts are just really new to you but I hope that gave you some good exposure to how they work [Music]
Info
Channel: Learn To Code
Views: 23,677
Rating: undefined out of 5
Keywords: golang, interfaces, go programming language, golang interfaces
Id: gfoVLXQ5ujM
Channel Id: undefined
Length: 20min 15sec (1215 seconds)
Published: Tue Mar 20 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.