STOP! This Is How You Structure Golang Applications

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is up boys and girls in this video we're going to talk a little bit about goaling folder structure how to basically uh set up a project uh and how very important how to stop being confused and start solving or tackling the root problem instead of creating problems from tener right but before we continue you know what is up you don't need to subscribe but you could give me a thumbs up or a comment um and a comment a comment in the comments right and of course jump into Discord community and check out the patreon if you really want to level up and there's a lot of good like right now you can see there is no light there is nothing almost in my house because I'm moving but in April we're going to start with good stuff right good stuff for people I want to learn engineering goal and distributed systems all that stuff microservices Docker the whole the whole Shenanigans is coming up in April so basically uh yeah you actually need to subscribe so you don't forget right uh so full structure very important um what I want to talk about today is that the reason why I'm making this video is not because I like it because I think it's it's just uh for me a waste of time but hey I just want to help you guys the problem is that Mi streams in Discord uh on YouTube comments people always ask can you please what do you think about clean architecture DDD um hexagonal Architects and all that stuff and to be honest I actually have no clue what hexagonal full structure is or clean architect or clean clean clean voltage I don't know what it is I swear to God I'm 36 of programming for 15 years and I have no clue what it is uh but hey so of course I looked it up and all of these terms all of these um these these confusement uh Shenanigans they have their pros and they have their cons right it's it's it's no at all but the thing is if you are a new developer or you have a little bit of experience you're coming to go or but actually whatever other language it is it doesn't really matter your most of the time confused because there is so many things out there Tech Twitter YouTube all these these these content creators they come up with some with some all these these fancy terms on how to structure and how to name things and that the only thing it does basically is it this is it distract you and it confuses you and instead of solving your root problem which is basically the the the purest form of programming is just solving your root problem right you want to build an API for a customer or you want to you want to make a game or whatever you want to build it doesn't matter that's the thing you should focus on and not on oh I need to make forward heat and maybe I need to make a folder here and where do do I place my types and my users and my authentication and my database and my interfaces and before you even know you're in a deep deep Rabbit Hole of problems and you think you are programming but it's not right you are not programming you are confused you are not a programmer you are a configurator right that's not what we want of course when experience when experience comes in you will already know up front which things will belong where and I will give you an example for example so let's let's take a look at this golang project which is basically completely nothing so the first thing we're going to do is uh go multitish and we're going to call this um I always do github.com and the M I'm gonna call this go folder because it is what it is the next thing I do in my goal projects is I'm gonna make I'm gonna touch actually a make file right I'm gonna make a make file here it's not mandatory but I'm always doing this because I don't know it's just convenient right make file and let me do ability you could say go uh build or you're going to say bin I'm going to call this uh app it doesn't really matter right and then you could say in a run gonna depend on builds and we're gonna do dot slash bin app and then I always do a test which is going to be go test minus V for both you can skip that if you want the whole shebang and if you really want you could do a count as one so it basically means that each time you're gonna make test it's going to not it's just for caching otherwise it's going to be cached and it's it's annoying sometimes you want to test risk conditions or something or someone these little nasty these little nasty rats in your program and you can run it 10 times and it's going to work with the 11th time it's gonna fail and of course if you're always hitting the cache you need to go into your test modify something it's it's everybody a lot of people already will know what I'm talking about so count minus one and then you could do a minus minus raise or something whatever make file is done right the next thing we're gonna do let's say we wanna build a simple API of course I'm not going to write it out because then we already here for 40 minutes and the attention span of the the average YouTube is is 10 minutes so we only have five minutes so we're gonna make a new file here and we're gonna call this main.go exactly right so we're going to say package main heat and then we're going to start off our Funk mean and that's it right so you need to think about a programming how does how does a program actually work you're going to create a binary and that binary is going to start from somewhere and that's the main function right so that's basically what's what what will be in the main everything that's going to bootstrap your program right of course A lot of people are using another folder the folder CMD and and CMD will be a main and all that stuff but that's already it's already it's already too complex right I want the Nobel Prize of simplification and I need to that's not for now the CMD folder with the main no need package mean Funk mean perfectly fine right so what we're going to do in Main is actually we're gonna boot up I'm gonna mock this out right it's gonna be a little bit of pseudocode so don't take this too seriously you're going to say HTTP I'm going to do a listen and surf fish and there's going to be a port 3000 and this is going to be a handlet nil heat and if you really want to make it fancy you could say a listen added read or actually yeah listen to this fine a listen Adder is going to be a flag um string oh insane lag heat uh you're gonna call this a listen adish listen add it like this then I'm going to give a default value of 3000 because it's the mega of course and by the way what I always tell you is that how higher the port the higher the skill level you have so in my case I could do something like 40 000 and one or actually to be honest 999 right it's basically almost a maximum skill level it's it's the level cap right um and then this is going to be some some explanation we don't care then we're going to say a flag uh Parts here just like that boom and then we can actually instead of this 3000 hardcoded Shenanigans we can dereference this listen at this because golang makes this we're not going to discuss this but we need to dereference this listen Boop that's fine and then basically uh you're gonna have a problem we're gonna have no problem you could do a make run here and it's going to build and it's going to run or serve it and everything is fine right you see no folder structure no Panic no Shenanigans we just take the money on the beach right okay cool so that's fine of course uh we have also we need some handlers right so basically you're going to make a simple API you want to get a user well then you're gonna say HTTP handle thank you handle funk uh and it's going to be a slash user or something right most of the time you're going to use a good gen or a fiber or an echo or something it doesn't matter right what's best what's wrong what's better what's bestest nobody cares just pick one five it fine Echo also fine you know don't worry about it just pick something and if you really want to be a badass chat you pick nothing and you quote it yourself depends it doesn't matter right it doesn't really matter okay so we say handle Funk usage and then we're going to say handle get usage that's going to be I'll handle it of course we don't have a handlet now you're going to be panicking no problem we're gonna make this Handler in our main it is what it is so we're going to see you could choose you're going to do it above the mean or below the mean it doesn't matter right you're going to say funk handle get use it right and that's going to be HTTP a w a HTTP response uh writers and then appoint us to this um man long time that I did that I did this HTTP a request and there's a lot of lag also boom just like that and if you can type it could be nice boom handle guide usage you see it's already taken care of and maybe you're going to have a handle get account no problem boom handle get account duplicate this change this handle account here boom and then we're going to say heat handle get account all fine so now in that case types for example right you're going to have a user type what you could do is basically you could say okay I'm going to create my type heat use it it's going to be a strict right just like that so you could use user and your things here like this user is going to be this user heat and then of course we're going to do this so the compiler is happy right perfectly fine then you could say I'm going to duplicate a structure code right guys you understand what I mean just give you an ID and this is going to be used in account right it's going to say ACC account and of course we're going to make it again for the compilers right again this is super pseudo just to give you uh an idea right so basically we and of course you could have some database stuff right you could make a database but you can already see that we're gonna have our handlers or types and our database stuff um is going to be in the main file and for me personally that's okay it doesn't really matter because by the end of the day you're just going to search for stuff with with uh which are fizzy finder or something or with your search tool or you're gonna GD into stuff um or are you going to scroll with your mouse it doesn't really matter right folders right folders basically guys think about this what is a folder going to bring me as a benefit for solving my root problem right this is an uh for my job for example I need to make a very simple uh service API that's going to get a usage or something I'm going to do some data manipulation it doesn't really matter and then account and all that stuff and that's it and I need to uh represent it as a nice Json response right and do some rate limiting and yada yada yada and some blogging and and all that stuff what is a folder going to help you make your program run better make your codes be more robust nothing the only thing it's going to do is create extra problems create possibilities to come into circular dependencies creates problems when you need to refactor it's going to create so much so much extra shenanigans that that does not need to be there right of course something that I'm actually always do and that's my types right and that's what I want to share with you it's these types because most of the time when you are in a day job right you're going to make these Services right in golang most of you're not going to make monoliths that's not going to happen right if you think you're going to build a nice monolith uh in golang with all controllers models then then I think you're still in a dream uh in a beautiful dream with unicorns and and flowers that smell like uh put Puri but but that's not the case right so you're going to make a service and your service will basically need to be um accessible by other services in your company right and that could be services from python that could be Services written in Google itself it could be from from the darkest deepest holding program it doesn't really matter and most of the time when people are writing in go they probably need to interact with your um with your service with your API and it would be nice that also these people sitting on another Island oh they're just standing up right now you see they are in a meeting we don't because we don't do that the only thing we do is stand up whatever I'm gonna cut this because I have no time to cut to cut videos uh so basically it would be nice that these guys that are standing up there have access to your user in your account so if you put this into the main you're already because it's impossible then they need to duplicate that stuff and it's going to be a mess because if you update your usage uh they need to update it and it's going to be a whole big problem so what you could do in this case is make a new folder and call it types right color types we make a new file and then you could say I'm going to make a usage dot go and then account.go and they all will have 20 lines of code I'll do I wouldn't recommend to do that that's not it's not a bad case you could do that if you really want but I'm not doing this most of the time I'm just placing everything in a types go boom and then I'm gonna say here it's going to be package types and then I'll say type usage it's going to be a strict just like that and then I'm gonna copy this and it's going to be an account here to Boom because a lot of people also think in the model controller things which is is it better there's no such thing as bad as good like uh I already said that but I don't like to put a lot of coat inside of this type stuff I think these types is basically just data right a more of a data oriented Design This is a user structure and the only thing it has is is data and some Json tags or something or or whatever and that's it right and if we want to do some operations on our usage we're gonna make a function that will take in a usage and we'll do some manipulations there and that's going to be you know handlers or another thing and I I don't like it to put it into this types package but that's also a personal preference right so cool so the next thing we're going to do here in main instead of these things heat what we do what you need to do is we're going to say type c types use it heat and of course types of count heat and it's already been taken care look at this how beautiful right so we have our Handler's heat we have our bootstrap heat and and we can just do a go uh a make run and our API is running of course what you also could do is instead of this main.go if you really want to make this bootstrapping functionality you could make a new file and you can call this handlers handlers.go right and then we're going to split the screen real quick and now we're gonna basically copy this handle thing here delete this uh paste it in paste it here and then we're going to say package mean because we're still in the mean right it doesn't really matter just like that and it's all going to be fine we're going to delete the Heat and now we can delete well let's open up this thing right so what we have now is basically we have our main is just a nice bootstrapping thing it's going to listen to arguments here it's going to make our routes there is no need to make a routing or something just put them in here it's perfectly fine and then at the end we do HTTP listen and Surf and you should basically lock fatal out or something it uh or something else right that's that's for you to do and now we have our handlers here which is all these handlers are here of course this is just right now right this is how you start this is how you solve your problem and let's say right now we have solved our problem and you don't make a package test that's very important don't make a package test make your tests inside of this thing right just just make this new film and you say handlers handlers test boom and you say package Main package mean Funk test boom make test ing everybody happy right very simple of course we could this is a very simple service we just have two routes exposed the get user and get account but you could imagine you could imagine that you have a a much larger project right a much larger cool project and then of course it could be a little bit nasty to have all these these things here but that's no problem what you could do once your problem is solved what you could do is you could say okay I'm going to make a new folder I'm going to call this API right you're going to call this API and then you could say handlers dot go here boom right and I'm going to refactor right so we're gonna open up these handlers here uh actually to be honest let's make a new file use it Handler or something use a handle.go and then this guy is going to rename this uh accounts handler.gov there's going to be package package API delete this paste it in boom handling it is we're gonna expose this guy handle get usage perfectly fine HTTP types of users all good nobody is complaining the same thing with uh oh this was the use it Handler made a mistake no problem you see you see how it goes we have folders and their problems arrives arise out of nothing package API boom boom account Handler where is this thingy copy this paste this in boom save that this Handler hit we're gonna delete this boy he's out Handler test he's out uh of course here in API very simple new file accounts Handler test.go boom and the same for users you get the point that's going on here boom save here and the test is going to be a problem package API funk test boom simple copy this here bum make a new new file gonna do it good right you're gonna say user Handler test go paste that in save it call it a day take the money and set Margaritas on the beach right of course we have duplicated tests not anymore right easy and then go of course it means you have a problem here because we have handle users no problem you're going to say API and handle guide users and the same thing heat API boom handle get account save this thing give vs code some time and it cannot figure it out what's going on you see no problem you're gonna say get up and Yem go folder I think API save it and call it a day of course we have a problem with handling that account for some reason I don't know account Handler sheesh I don't get account of course we need to expose this uh little little boy heat or girl and then Main boom look at this perfectly fine we're gonna collapse this folder sheet we have API binary don't forget to put this in your get ignore we have our type C's and then our bootstrap heat right Uh something's going on here but it's fine you see that's this is already advanced for me right this is already I would keep it in the main especially for for a smaller projects but if you really want to be foldery and you really want to have the feeling you're a configurator sometimes it gives you dope I mean you can do it like that and this is basically already a little bit built for scale so because you have your API folder you could add all these files book handle if to do Handler whatever you want right you get the point and if you really want you can make another folder database or something and put your queries in or whatever right that's the thing right so this is basically it uh like I said it's very important what what do you need to take away from here is that you need to focus you need to stop uh thinking about all these fancy terms because by the end of the day it's getting compiled and the only thing it matters is that your root problem is being solved because all these terms they have their pros and they have their cons their use cases for sure but you need to make sure as especially as a beginner programmer or or uh also they have a little bit of experience two three years doesn't really matter focus on your problem build start small and scale up when needed but make sure you solved your root problem first all right cool if you like these videos consider subscribing give me a thumbs up leave some questions in the comments and jump into my Discord Community 24 7 open um hey free to join any race and any gender we are a family and you belong there yeah don't forget to check the patient also and I'm looking forward to seeing one of my live streams like I said a little bit busy right now uh in my life because I'm moving and all that stuff it's crazy and but hey I will manage it I'm gonna deal with it and in two weeks or in three weeks I'm gonna drop um some cool stuff and in the meanwhile I'm gonna maybe small videos just to keep my YouTube channel busy bye bye
Info
Channel: Anthony GG
Views: 20,783
Rating: undefined out of 5
Keywords: golang, golang folder structure, go project structure, golang tutorial, learn golang, golang for beginners, go programming language, golang best practices, golang tutorial for beginners, golang beginner tutorial, go language, golang project, go programming, golang programming, go programming tutorial, go tutorial, golang project structure, golang tutorials, golang hexagonal architecture
Id: eSDYl-RuHjc
Channel Id: undefined
Length: 22min 3sec (1323 seconds)
Published: Tue Mar 14 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.