Don't Make this Golang Beginner Mistake!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so after doing a lot of reviews recently I see that a lot of people have a very good grasp of goang because it's a very simple language very easy to read and very easy to write but I see people making the same mistake over and over and it's not basically a skill issue it's just something that you need to learn and if nobody tells you it you will never learn that's why I'm here at the total code is my private community and each week I sent out an assignment that you can solve right you can solve that in any language as you want but most people solve that in goang and by the end of the week most likely on Sundays what I do is I basically uh do a private live stream for the members of that Community where I review resumes but I also review your assignments or other projects so this is basically an assignment of somebody from my private Community he did a very good job but um he made a mistake that I see a lot of people making especially if you're new to golang it's nothing to do with your skill issues it's just something you don't know and you need to learn that right and that's why we are here so uh hey if you're interested in the total coder Community you can uh find that in the link down below all right so let's get started so the previous assignment we needed to do something with loog ingestion uh and he his implementation was more of a Kafka implementation and let me show you what's going on here so we are producing some Json data which is representing a log file and we produce that on a Kafka queue right and of course if you produce on a Kafka queue we're also going to consume from that queue and basically this is the Kafka consumer structure which holds some data which holds some some variables here and the problem is hereat right so very simple it's going to consume heat it's a for loop it's going to read a message it's going to basically Marshall that into an log data because the assignment was hey we're going to provide you some log data and you need to basically consume that and store that efficiently in some kind of a persistent store you can choose whatever that is mongodb elastic search whatever right you could pick whatever you want and the problem is here right so because this is a loop right every time we consume he's going to actually two mistakes here each time we going to instantiate a new mongodb client right so we're going to make a new mongodb connection which is not a good practice because if you're producing a lot of logs and you're consuming a lot of stuff you're each time going to create this new client which is basically a little bit of an Overkill right and once that client is basically instantiated we are going to insert that into a DB and send the A and call it a day so from a first inspection of this scol there's nothing really wrong with it right it works perfectly fine uh just well this could be more optimized like I mentioned this client but there is basically a little bit of a structural problem here right because what is going to happen if we want to say Hey listen right now we do uh a Mongol persistent store but what if we want to use elastic search or we want to use something else right uh we want to write it to dis or we don't want to do anything at all because we're in a test phase or something we want to just have a uh we just want to log it out or something we don't want to persist it so if we want to do that each time we basically need to or we need to write a new function or we're going to basically just comment this out or Del it and make a new implementation here right for example elastic search or something so you can see this basically a problem a structureal problem actually it's a dependency problem why well because this client here this moongo client is completely encapsulated into this consume function so it's a very hard dependency right this kka consumer depends so hard on this client that it cannot yeah it's just a very hard dependency it's not an AB abstract type right so in goang how is a very common mistake and it's not only this is just a Kafka example but it's basically just the repository the DB um dependency a lot of people tend to forget to abstract way right so how can we solve that well it's actually very simple right I'm going to just do some some pseo code here instead of basically creating your moo client here what he needs to do is it could make an interface for example uh a persistor actually it it's locked so we could do something like a log persister and it's going to be an interface and what could be a good interface design here it could be something like persist um persist log for example and as a good goaling engineer we're going to give this a context context right and then because it's an interface and especially actually in a lot of functions you want to have a context as a first argument but espe specifically for these interfaces because um yeah a lot of these thirdparty libraries like MB or or elastic search or whatever they use context right so if you have an interface that basically supports that that's basically a big win and it's a very very good practice to do so persist log and we going to have I think it is data log data something like that right and of course an error right this could be the log persist interface and the only thing we need to do is say Hey listen we're going to actually embed well embed we're going to uh attach is also not a good word how do you call that we're going to basically provide this persist you could say uh persistor for example can I actually write this persist is going to be a log persist interface right and if you want to for example new Kafka consumer it's going to take a Kafka config but it's also going to take for example the uh persistor which is going to be the log persistor right and then I'm going to say persistor here is the persistor that we provide as an argument which is basically H some kind of dependency injection right there a very big rabbit hole we can go into this uh but that's what I call dependency injection with this interface provided into this Kafka consumer structure and right now U we basically completely abstracted the way how our persist is going to persist here right so we can actually completely what's going on here we can completely uh delete this thing and instead of connecting it's already connected right it's already in entiated uh because you're going to provide that in the new kka consumer uh constrictor function whatever you want to call that and the only thing we need to do is basically say if R is going to be uh is the c. persist do persist log and we're going to take in a context do we have that yes we have one that's already a win the context here and then we going to put in the data which is the log data here and then if the error is not n we are basically going to copy some stuff from here like this copy that paste that in delete this and save and that's going to be the code right of course we need to change some other stuff uh because yeah right now we have an extra argument here in um new Kaa consumer but it doesn't really matter right this is completely out of scope so right now you can see that if we want to consume it's just going to call the abstract the interface basically from the persistor actually look at that I'm making this small uh typo mistakes here persister uh just like that and of course we need to pers look at that man the pers just like that voila now it's perfectly fine so right now we don't depend on a specific uh persisting type like MB or elastic search right now we just basically depends on the interface and anything that has the persist log um function or method attached to it uh basically uh is eligible to uh be inserted as this argument in a new consumer can be elastic search could be Mong be could be whatever you want could be a no op it could be uh whatever you want right as long as it basically uh implements this persist log method so and this is a very common mistake uh of course the code will work right the code will work like he did but like I said if you want to do an interview assignment and they see that you basically understand that you need to abstract that out um you're going to have yeah I think you're going to have a more better chance than people that do not do that right so let me know if you make the same mistake or uh that you learned this or whatever let me know in the comments what you think about that and of course hey if you want to learn more about goang like I said join my Discord Community or join the private Community or buy some of my courses whatever uh that's all up to you and I'm looking forward to see you in one of my live streams or next videos peace out
Info
Channel: Anthony GG
Views: 20,403
Rating: undefined out of 5
Keywords: developer, software developer, golang, golang kafka, golang beginner tutorial, golang tutorial, go tutorial, web developer, software development, programming, go tutorial for beginners, golang interface, golang interfaces, golang interview, learn golang, development, dev, vscode, tutorial, golang for beginners, go programming tutorial, golang course, go programming, backend developer, interface programming, golang tutorials, web development
Id: M9h6KGFRRwE
Channel Id: undefined
Length: 8min 56sec (536 seconds)
Published: Tue May 28 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.