This Is The BEST Way To Structure Your GO Projects

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video I'm going to show you how you can easily install go and what is the best project layout for your applications [Music] I'm a go developer at twitch and I've been getting a lot of questions from all these different people who are interested in joining the Gopher Side Learning goaling recovering rust developers and they want to join this side but they don't know where to start so in this video I'm going to describe three things that I think are crucial for any go developer the first one is where you can find all the release notes on any update major minor or patch for all the goaling programming version releases while also showing you can find the best resource to find any package information within the go ecosystem secondly I'm going to quickly show you can install go because it is easy because it's go everything is very easy and lastly I will show you how you can scaffold your project and use the main conventions that are pretty much accepted within the go community and if you go well I put a whole video it should pop out somewhere in the quarter there where I explain why I truly think golang is one of the best programming language to learn right now for a multiple different reasons so the so the first thing I want to quickly show you guys is the great official website go.dev and the releases I use this anytime there is a patch update a minor version update and of course a major version update but that always follows a blog post or something like that you'll hear about it but here I get all the patch notes that I need all the Mind releases I need it's a quick read and the second resource I need to show you is about packages so again the official website pkg.go.dev if there's any package that you're curious about or think is in the go ecosystem this is the place to look and search for it let's say you want an application as JWT so you type JWT and bang it pulls out all the packages that you can use and import that are publicly available for you to use in your app there's some developed by gold and this is the one I recommend by the way and it has everything you want it has examples usages methods documentation it's all here all for you to peruse and enjoy and it's just so awesome that you don't need any other website to see weekly downloads or Imports anything like that you can just type and go to the official website find what you need and move on with your life all right cool so you're saying this is great the websites are awesome but how do I install this well luckily for you again all you got to do is go to the official website go.dev doc slash install you click this big blue button that says download and it shows you the major minor and patch note version 1.20.6 you click it I already have it downloaded then you follow the instructions if you're on Windows or Mac or Linux which I am because I use Ubuntu you just follow the instructions so for example this one is remove any previous go installations and run this command using sudo so I'm just going to go ahead yoink that I'm going to open up my terminal and you can already see I do not have go install so then I just run go run the command and Bing Bang doodle sauce I will have it but before I do make sure you include this export path in your dot zshrc or bash RC I don't know what you're using whatever your config profile thing is go ahead and put this there and now you are truly ready to rock and roll confirm your Go version and there it is 1.20.6 all right so now you have all the resources you need to find information about the packages and your release notes and even know how to install go how do you define the project layout Melky we've been asking please so goaling doesn't actually have a strict enforce guideline however through the years the community have adopted kind of the accepted rules and practices when you scaffold your go project every gold project is structured roughly the same it has the following components as a root directory there's a bunch of non-go files you could go dot mod your readme your licenses your make file those will all go typically at the root of your project and then you have your CMD folder which can contains the subdirectory of your application now within the subdirectory there's going to be a main.go file and this is the most important file of your entire application because it's the entry point this is where all the logic that you will write in every other folder gets imported and used and executed in your main.go then you have your slash internal and slash package folders and these basically serve the same purpose they hold the core logic of your application your business logic internal and package will hold things with like your client your data your validators your middleware all of that and that will be imported throughout your application and to your main.go file the main difference between internal and package is that internal keeps everything private and as Library code meaning it will only be available to your project within that module lastly we have our bin directory which will contain the compiled application of our binary code and our test folder which will have our test code but I you guys don't write that I know that all right so now you can see here I have this empty project absolutely nothing in here the first thing I want to do is go mod init and then you can call this anything you want the typical convention is something like github.com Melky and then the name of the project the go project structure now this is super important because this is how you're going to actually import everything from an internal or package folders which I'll explain in just a second next up we're actually going to create some of the scaffold for our project we're going to create a bunch of directories we're going to make a bin directory a CMD slash go project structure director and subdirectory internal and task to go ahead and do that and last let's go ahead and touch CMD goldproject and Main dot go bang now you can see over in the corner here everything defined for application you can see CMD internal tests go and make file so this is the basic layout and scaffold for your go application so when you open up main.go it's going to start airing out the thing you just need to do is declare package Main and you're going to be fine so just declare that and now we can start working on other things in this video I'm going to create a simple API that you can curl and server and go and I'm going to actually declare all the route all the Handler functions within the internal subfolder under a route sub directory so if we go back into our terminal and CD into internal it's going to be empty I'm doing clear to this I'm going to actually create a new directory called routes and then I'm going to go see the into route and then touch route dot go okay so you can see here I quickly wrote some code from my routs.go file all this does is it's using the internal packaging from the internal go library and it's just declaring a new router which has two routes here one is the Home Route which goes to index Handler all it does it says welcome to the home page line five I have slash API slash data which has pretty much the same thing okay now back in main.go I just again quickly split up some code nothing fancy here it just spins up a server however one thing I do want to note here is this routes.new router so this was declared in our route.go file here you can see this functions new routers declared it's all capital so it can use it but I'm going to complain here that's undefined so if you remember when we defined and declared a go mod in it at the root level of our project we declared as github.com Melky slash go project structure this allows us to import things from our internal folders or package folders freely and throughout our go application and you can use everything it doesn't have to be github.com milky slash whatever you can just write as backend or service or the name of your application and that will work as long as it's consistent and this is a very strong point because goal link doesn't have the concept of relative Imports it all comes from your go.mod which is typically declared at the level of your project so now let's test this function out so if you go go run CMD gold project structure you can see here server listing on localhost 8080 which basically says that you know this main function works so now let's curl it so open up a new terminal and type in curl localhost 8080 API slash data run that and there you go some data from the API super easy nothing to it and that just showcase how you can actually import logic or business logic into your main.go when you are creating your go project there's a few things to keep in mind and I try to look at these five main Concepts the first one is keep your binaries and libraries very separate that kind of rhymed like I explained the importance of the intro and package folders is that's where you have the Crux of your logic that gets imported into main.go and that gets compiled into binary code the second point I'll discuss is avoid nested packages keep keep the packages flat and what do I mean by that well if you're using internal and you have like I said the route dot go put that in a route subdirectory so it's internal routes routes.go do not Nest it if you have something else that goes with route do not make a separate directory just add a different file within the route subdirector even add it to the routes.go file that you already have the third one is pretty simple keep your package names descriptive so if you have routes making routes.go make it easy to read easy to follow if a subdirector called lambdas then you should have the following Lambda files very easy to follow and very easy to use within your main.goal or anywhere else in your go project the fourth one I already talked about is put your non-go files at the root of your project like make files like licenses anything like that keep those at the root of your project sometimes there are exceptions but typically the best rule of thumb keep that at the root and lastly keep your test and folder I can't even say that with a straight face I know you guys don't test I know you I know you you guys don't do any testing I don't even need to talk about this that was a waste of a folder created so I hope you guys enjoyed this video let me know what you think of all this go content are you enjoying it are you liking it is there something you want to see me make with go anything anything you want me to explain any concept you want me to go deeper into I thoroughly enjoy making these videos and I hope you guys enjoy watching them and if you do make sure you comment like And subscribe we are so close to 5K subscribers but as always you guys have to make sure you write some Goku tonight and lastly and you guys all remember this one oh you gotta power it
Info
Channel: Melkey
Views: 65,393
Rating: undefined out of 5
Keywords: video sharing, video, sharing, computer science, software engineer, silicon valley, computer programming, coding, learn to code, machine learning, artificial intelligence, cloud, python, javascript, how to code, Data scientist, AI developer, Machine Learning, GoLang, GoProgramming, GoProjectStructure, GoLayout, Learning Go
Id: dxPakeBsgl4
Channel Id: undefined
Length: 11min 8sec (668 seconds)
Published: Thu Aug 03 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.