A Practical Example How To Use Interfaces In Golang

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
interfaces are still a bit of a problem people are still a little bit confused especially on when to use them and how to use them so basically what I have here is a simple application um that we are going to inspect and refector into something that utilizes interfaces so you know exactly when to use them in your path of becoming a goang engineer all right but before we start if you're not yet subscribed to the channel guys consider subscribing give me a thumbs up and leave some questions in the comments jump into the Discord Community Check out my courses about goaling if you really want to become professionally active as an engineer thank you very much all right so what is this application it's a very simple mock application where we're going to create an account and uh a simple API right we have this uh goang serve M we have the post SL and we have a Handler uh which basically is going to create an account how are we going to do this very simple um we're going to post a username and an email nothing too fancy we're going to decode this into an account structure uh probably here you're going to do some logic to create the account database stuff it doesn't really matter right it doesn't really matter and after that we are going to notify that the account is created right this a very common practice I use this all the time where a user does something and then you're going to maybe you're going to notify the user you're going to notify other users you're going to notify an admin over email SMS slack Discord whatever some kind of a notification system that's going to be triggered after a certain action in your application right and then we basically just going to um return the account that the user has created right a very simple mock thingy a very important thing to note here is the notify account created has a Time sleep 500 milliseconds why because it is an IO operation right you're going to do a get request you're going to do an email and SMS you're going to use some third party Shenanigans which basically means ah it's it's going to take some time right 100 200 300 1 second uh we do not know how long it takes so we mock this right here right and if we test this basically let me see if it's running it is uh we going to send this boom it's going to username GG email Anthony GG blah blah blah all simple and then we're going to see that it's being locked this is the notification system which is just a lock for now so if we take a look at this code um it's very simple and actually there is nothing wrong with this code however let's take a look at this notify system here right so if we take a look at notify account created there are some different ways we can notify in this case it's just a log it's easy peasy but what if you want to do an email right or what if you want to send an SMS or boat you know what I mean or you want to use a Discord Handler or something uh Discord hook or maybe a telegram uh hook or even a slack hook you know what I mean the this notify can have multiple implementations right and depending on where he's going to use this uh we're going to select some kind of an implementation maybe uh in development or on staging we don't want Discord telegram or slack maybe we just want a new up Notifier right no operation Notifier just logging something you know what I mean so instead of basically modifying this piece of code heat all the time right and maybe you want to say oh today we're going to use the email next week we're going to use the slack because some manager asked us to do so so you're going to basically change this function all the time uh refactor it comment it out comment it in you know we all been there that's not a good approach so we're going to refactor this to use interfaces because I think this is a very very very good use case that is actually simple to grasp for beginners um and something you that maybe is going to click is going to uh snap snap some wire in your brain so you never forget all right so the first thing we're going to do if you want to make an uh if if you want to refa this to an interface is actually create the interface and think about the design right so let's go to the top here we're going to say type uh let's call this an account Notifier right uh which is going to be an interface right and then we're going to say uh maybe the function is going to be notify account created this is the function the question about it is what's going to be the signature what do we know well we know in goang returning an error is always a good use case maybe for the no operation Notifier we don't need an error right we going just log it out so we can return no but it's always good practice to return an error because maybe other implementations like the email or the SMS Notifier they will interact with third party shenans so returning an error is very appropriate to do so we're going to do this eror that's something we know already and then what we also know is we need an account here right because if you want to notify that account is created we want to have the details of this account so we're going to say the account but very important we need to inspect again here is this notify account created function we have here that we're calling here right so it's going to sleep for 500 milliseconds in this case which basically means that if we're going to interact with third party systems right we don't know how long it's going to take it could take 200 milliseconds it could take 50 milliseconds it could take 10 minutes we don't know right what if our email provider is out of for example recent they they basically bought their own database lately and you just could not do anything so if you do not have some kind of a guarding mechanism I call it you need to make sure if you build an application it's be it's deterministic in all ways also in time and what I mean by that is you can never know how long it's going to take but you can limit the maximum amount it's going to take right for example okay if you want to use the email service it can take two minutes and if it takes longer than two minutes we're going to cancel this out right because if you're going to use this in a go routine and you not cancel this yeah you could have some memory leaks you know what I mean you could have some clutter so how do we do that well a very good practice if you design something like this is to basically say we're going to use a context context right boom just like that so we have our notify account created function which has a context and an account and returns an nrow I think that's already a very solid thing so the next thing we need to do is basically uh we need to find a way to use this right because we could actually create the NF fire here but that's basically the same problems we want to have some kind of a quote quote dependency injection mechanism and people oh dependency in something like that right I don't call it like this to be honest but people understand it that's why I'm calling it like this right so in able to do that we need to have we need to find a way to attach some State on our Handler right because this Handler here handle create account is actually just a function right and we want to have state how do we do that well we're going to change we're going to make a struct here we're going to call this an account Handler uh which is going to be a structure and that is going to have some State we're going to say what does it what does it need it could have multiple things it could hold your DB it could hold whatever in this case it's going to hold or account Notifier which is an interface which is the account Notifier just like that okay and then we're going to just attach this uh function method uh to this uh Handler right the method receiver as you call this we're going to say the Handler we're going to make a pointer here uh account Handler handle account created beautiful right so instead of basically calling notify account created this is function we are going to say actually we are going to say um let's delete this we are going to say h. account Notifier do notify can't created right and what do we need in here we know it needs a context so in this case we're going to say our context here yes just like that and then we need the account which is this beautiful thing we created on top here right this thing here you see H that's it right that's the thing we are going to do so nothing too fan see the next thing we need to do basically is here uh we need to create our Handler right we need to create an instance I don't like to call it like that but people coming from other language they understand call create an instance of it right so we're going to say that the account Handler is going to be uh let's make a pointer here it's going to be the uh account Handler and now we need an account Notifier which we don't have yet right so I'm going to basically uh comment this out here uh well leave it open doesn't really matter let's create the implementation right because account modifier is the interface now we need to create the implementation right so for example let's make a type a simple uh account Notifier it's going to be a structure here right and U let's do something like uh let's no State because it's a simple account notifi we're going to we're going to do not attach any state to it but you could imagine you have something like an email notified of an SMS notified you could have some state for example an API key or something else or a secret or whatever right um but we need to the only thing we need to make sure is that this simple account file let's copy this uh is going to be Funk uh let's call it let's call it n Whatever paste this in simple Notifier and it needs to basically implement this notify account created so let's do that notify account created we need the CTX which is going to be a context context what's going on here a context context and an account which is going to be this account thingy and also an OT right boom boom boom let's return n here because it's a simple account modifier no arrows to be handled and let's actually do let's copy this thing actually no let's do it this let's do this as loock info uh new account created let's do account uh that's username actually to be honest let's do something simple it's going to be uh account username and then say email is going to Beil something like that very simple right of course if you have an email or an SMS Notifier you're going to do some some other stuff here then you can actually utilize the context package to do some cancellations uh I will link a context video in the description right so you 100% know how to use that and now we need to basically create here a Notifier which could be an a simple modifier like that and the only thing we need to do is we're going to say that the account handler do handle uh if we can type here account Handler handle create account and that's it right so basically if I cleared things out I'm going to go run main. go just like that and I'm going to post this again you see that it basically just work like new account created username email uh the exact same thing it's working perfectly fine right so the logic stays the same but our application is a little bit more goaling ideom medic it's a little bit more uh composable is maybe not the the right word but it's a little bit more dynamic because um instead of basically going to this notify this can be actually deleted guys right because we don't need this anymore just like that so the only thing we need to do instead of going into our Handler right instead of going into our handle create account here and change our logic in here right instead of changing our Handler the only thing we need to do is just pass in an new kind of implementation of the account Notifier and that will basically completely change the behavior on how we notify right you could use a go function whatever it doesn't really matter it's just an example guys I know already what you're thinking why we not use a go function yeah you can you can but this is just an an example uh for example let's say this is simple account modify let's make whatever we could do let's copy this whole shebang heat and we're going to make this um uh how can we call this uh a better a better a better account modifier I have no clue guys just uh let's change it everything up here better account modifier and a new account created uh by the better Notifier I don't know I just want to show you Notifier right just like that and then the only thing we need to do is basically instead of uh saying that we are using the simple account Notifier no we are using the better account Notifier right and then if we basically clear the screen go run main.go boom just like that and actually it's in here and we sent now we can see the new account created by the better Notifier so that's how easy we can swap this right um better for testing it's just better right and of course people are sometimes over engineering with interface they are using them too much in this case I think it's a very very very good appropriate use case for interfaces and I hope this basically um lighten up a little flame inside your brain but hey uh so you can actually have a better grasp on when and how and why to use interfaces in goine but you can also do do this with some kind of a functions and typed functions and whatever you want to do with it uh but using interfaces you can attach state to it uh for some implementations could be very important think about API Keys maybe a caching system or whatever right uh let me know what you think about these videos if you want to see more of these kind of uh videos where we refactor something into from something go to something a little bit more ideoma let me know in the comments um and I'm happy to make more of these thanks for watching and I'm looking forward to see you in my next video or live stream peace out
Info
Channel: Anthony GG
Views: 20,890
Rating: undefined out of 5
Keywords: golang, golang programming, go interfaces, golang interfaces, golang tutorial, learn golang, golang tutorial for beginners, golang interface tutorial, golang interface, golang interfaces explained, golang course, golang tutorials, golang for beginners, go programming language, go programming tutorial, go interfaces explained, golang beginner tutorial, go programming language tutorial, golang structs tutorial, introduction to golang, interface, golang example project
Id: McRq-uBAa9I
Channel Id: undefined
Length: 14min 42sec (882 seconds)
Published: Mon Mar 18 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.