Master Golang with Abstraction

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up everybody and welcome back to yet another video on the go programming language so in this video we're going to go over a concept that's going to potentially Propel your go development skills to the next level and that is to say that understanding this concept is going to make you better at reading go code as well as writing go code and this magical concept is called abstraction which is actually one of the pillars of object-oriented programming but it's also very useful in the go programming language and it's something that you're going to see being used throughout different open-source projects or work projects Etc but before we get into explaining abstraction I'd like to quickly go over the sponsor of this video which is me so if you're a person that enjoys my style of teaching I have a full course on microservice architectures using the go programming language which will be linked in the description below so if that's something you're interested in and you want to support what I do then feel free to go and enroll in that course but without further Ado let's go ahead and get into the initial explanation of abstraction using the go programming language all right so first of all we need to make sure we understand what abstraction even is in the context of programming so let's imagine that we have a person here and this person wants to get a drink from a vending machine so you'll have to excuse my terrible drawing but but this is a vending machine so this person wants to get a drink right and what does this person need to do the person needs to put money in the vending machine so money goes into the vending machine which results in the drink coming out of the vending machine now in order for the drink to come out of the vending machine the person has to put the money in and push some button right and the person expects that when they push the button the drink comes out of the vending machine but they don't care about the underlying Behavior like they don't care how it happens right like say for example we put the money in here here and then we push this button to get this drink now when we push this button we just expect our drink to come out we don't care about the technical details like there might be some underlying mechanism that's happening when you push the button to specifically spit out the drink that you're requesting but we don't care about the underlying implementation of how we receive the drink from a vending machine all of that is abstracted away from us that's what abstraction is like we understand that in the background there might be all of this complex stuff happening in the vending machine like the mechanics of the vending machine but of course the customer that's buying the drink from the vending machine doesn't care about any of those underlying implementation details so all of this is abstracted and all the customer sees is the higher level functionality we need to pass in the money as a parameter and we need to push the button which Returns the drink now some of you might notice what I did there I said we passed in the money as a parameter and it Returns the drink so you can think of this vending machine literally Al as a function so we'll say funk get drink and we'll pass in some money and there might be a bunch of code here which is the underlying implementation stuff and then the drink gets returned right now the function definition is this stuff that's abstracted right so as the caller of this function this guy here he doesn't need to care about any of that all he needs to know is in order to get his drink so we'll do my drink a variable called my drink and we'll just set it equal to get drink with the money passed in and we know that since this function Returns the drink we know that we can assign the result of this function to this variable my drink so that's abstraction so how does abstraction work in goang or in the go programming language well it works by making use of interfaces so let's go ahead and create that interface so let me just make this a bit larger and I have a main.go file here and we'll just go ahead and create the interface for the vending machine and we'll Define the contract for get drink so as you can see here we've defined the interface for the vending machine and within the interface we have the get drink method here which takes in money which is some amount and the brand of the drink and it's just going to return a string so for the purposes of understanding abstraction we have this package here VM for vending machine and the code for this will be in the description but you honestly don't even need to look at the code for this because this is the underlying implementation stuff that's abstracted from us right so I just want you to imagine that you're working on this this application here in Maine and you don't know about the implementation details of this VM package but you're just going to use this VM package and then we can just imagine this VM package is made by an entirely different team so as long as that team implements this interface the vending machine interface which means that as long as they Implement get drink then we don't care about the details of how they implement it as long as it takes in money and brand and Returns the string so from our perspective when we're working on our code then we don't need to even think about what's going on in this VM package so for example let's go ahead and just write up our code for our application so as you can see here our code is very simple so we'll create the struct for our application and that application's going to need to have the vending machine embedded and then we'll create this run method for the application and to create a new application we'll just do Funk new application and it's going to take in the vending machine which is going to need to be of type vending machine which means that the vending machine that we pass into new application is going to need to implement the vending machine interface so new application is just going to return the application with the vending machine embedded so we'll do return a pointer to application and inside we're going to have the de the vending machine embedded and I forgot we need to say that this returns a pointer to application and I'll make this just a little bit smaller here so now in our main function we can create a vending machine by using vm. new now this is the part that we don't need to worry about the implementation details for because VM is this package here and remember another team is working on this package so anything happening inside of this package is abstracted from us our team who's working on this part of the application so we don't need to care about the details of what's happening in here all we need to do is import it and use it since we know that it implements the interface so let's just go ahead and update this import here and now here we're importing this VM here and the reason uh the import looks like this is because the module is this here so if you go to the go mod file here you'll see that the module is github.com conton coding abstraction so when we import a package within this module in main then we need to put that as the prefix and then the package name here so this VM package we're importing it so now in our code down here so we have a vending machine here now so now we can create our application and embed that vending machine so we'll use the new application method that we created and we're going to pass in vending machine so now we can just run our application using that run method that we created and when this runs it's going to to use the get drink method provided by this package here now as you can see we don't see anything from our perspective about how git drink is implemented we know that it's going to return the drink and we're just going to print the result but we don't know how the implementation's happening here it's abstracted that's abstraction so from our perspective we don't know about the git trink implementation details right so we can go ahead and save this and we can run the application so so if we LS here you see we have our main.go file here and we can just do go run main.go and you see it prints out ice cold Cola so that's the drink that was returned from get drink now you've seen from this narrow perspective from the perspective of the people creating the application that that makes use of git drink how it works right so now let's switch our mindset we're now going to become the people that are actually writing the implementation details the code that's abstracting away from the team that we were just acting as that's working on this application here so if we go into the code here for the implementation and this is called db. go that's that's a mistake let's rename that to vm. go so in this vm. go file is the implementation for get drink so as you can see here the get drink implementation and simple it's just going to print ice cold and then whatever the brand of the drink is but what if we want to refactor the way we handle get drink so since our code is abstracted from whoever is making use of its functionality we can make changes to this without impacting that other team that we were just acting as and that's because when we make use of abstraction in the go programming language using interfaces we're actually also decoupling our code so when we were acting as that team that was working on main.go or whatever that code was completely decoupled from our implementation details here so actually if we wanted to we could change the implementation details here so we can do super hot and then brand and from the perspective of those that we working on main.go so let's once again act like we're the team here working on the code that makes use of git drink so remember the implementation details of G drink are abstracted away from us so we don't know that they made changes and we don't care we just we just know that we need to get the drink so we don't need to make any changes to our code right but also their changes aren't going to break our code so we could still go run main.go this time it's a super hot Cola that we get but we're still getting the drink back from the call to this method and any changes they make on their end doesn't impact our actual code here and that's because it's completely decoupled so when you think about abstraction you can kind of pair the idea of decoupling with abstraction as well and all of those are made possible in the go programming language using interfaces and that is how abstraction works in the go programming language so the main takeaway here is that we don't need to understand the implementation details of this vending machine package right so those details are abstracted away from us and what does that enable us to do that enables us to focus on our code like let's imagine that this run function is actually super complex so we'll just put there's a super there's a bunch of complex code in here so we don't want to have to worry about how get drink works like we we just want to be able to call get drink and somehow use it in this super complex code so it it kind of makes it the development process easier in certain scenarios like if you don't need to worry about the underlying complexities of methods that you're using from other packages then you can more easily focus on your actual code and what you're doing and if you write code in any language and you're making use of some external packages or whatever you're doing this all the time like you're always using some implementation from some other package but you don't necessarily need to know how those packages are working for example like a common type of package that's used in most programming languages is some package to handle math and arithmetic and stuff like that so let's actually go ahead and and explore that for a bit so if we import the math package here and we go down into our run method and we do math. absolute for example and we pass in 242 now what do we expect to happen here we expect this call here to return the absolute value of 240 of - 242 so if we go here and we just print it out and we open up another terminal here and run this again you see that we get the absolute value of - 242 right but we don't know about the underlying implementation of this absolute method here like I mean we could go to the definition here and we could dig deeper into the code or whatever and see how they're doing it but like we actually don't need to care like do like we all we care about is getting the absolute value so why do we need to know about the implementation details here trying to understand that while trying to make use of it in our own complex logic is just going to convolute everything so that's where abstraction becomes valuable like you can essentially abstract away things that are unnecessary for you in your particular use case to understand like from our perspective we don't need to understand how absolute gets the absolute value for us we just need to know that it Returns the correct absolute value for a given parameter and then we can make use of it in our complex logic so that's abstraction in the go programming language if you have questions feel free to leave them in the comment section below and if this video was helpful for you don't forget to leave a like and if you're interested in more content like this feel free to subscribe I make lots of goang content as well as content on algorithms and data structures and stuff like that so yeah I'll see you in the next one
Info
Channel: Kantan Coding
Views: 2,818
Rating: undefined out of 5
Keywords: golang, go programming
Id: CRY4_-p5FgM
Channel Id: undefined
Length: 13min 30sec (810 seconds)
Published: Mon May 06 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.