Golang Package Structure Explained

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
when creating eagle file declaring that the package is all you need to compile your program even without any functions or variables this shows how essential her packages are in go packages are a grouping unit of the code pieces and they help you organize your code they're like namespaces which is packages in other languages except packages in gold are to be small and and many because of this constraint of goha packages among others though developers are encouraged to structure her packages in a certain way the girl way first of all there are two primary types of packages executable packages and non-executable packages the executable package will contain a main function that denotes the start of a program and the only executable package is the main package so they are also called interchangeably every goal program needs to have a main package as the entry point to the program's execution to create the main package you can create a main.go file and the first line of the file would be package main indicating that this file is in the main package the file does not have to be named main.go by the way we'll get to that in just a bit second we have a non-executable packages also called it not a main or utility packages any package other than the main package is obviously a non-made package also a non-executable package they're not a self-executable they contain utility functions variables and etc that may be utilized by a main package to create a non-executable package you can again create a file but name it the package anything other than the main a package also when naming packages there are a few things that we must pay attention to package names should be short camel case and snake case are not recommended since they prom for long package names they should be descriptive for example you probably shouldn't have any packages and name your tools all non-main packages are categorized as utility packages so names like util don't indicate much package names should be the same as your directory name so don't declare multiple packages in multiple files under the same directory packages should be small and many and we should aim for less and nestings are for packages and directories one other main factor that influences how packages are structured is the scope of everything that lives in these packages there are three primary levels of a scope in go block unexported exported a variable for example can live inside of a block between the two hercules brackets this variable will only be accessible within this block now function blocks are also blocks otherwise this variable can live outside of any block and become unexported with an uncapitalized starting letter and that means that this variable can only be referenced elsewhere within the same package or this variable can be exported with a capitalized starting letter and can be now referenced elsewhere in a different package also note that a circular references are not allowed among her packages with all of these under consideration we should now be able to structure her packages in a more organized and educated manner so let's say now i go over a simple exercise first let's develop a simple user scenario say that we have a simple web application to get and create dojos and the associated ninjas the application consists of the following the main function that handles the setup of the router and the kickstarter of the server the handlers are for the request coming from the ninja path the handlers are for the request coming from the dojo path the ninja struct the dojo structure with the associated ninjas api requests and the api responses now the first option we have is to put everything into the main package like a noob but obviously we want to be a cool kid instead so we can structure the packages into three major categories as follows first we will have the handlers package the handlers package will contain the ninja handlers the dojo handlers which are logic to handle the requests coming in and the responses are going out second we will have the models package the models package will contain all of the models for ninja's drugs dojo struct requests and responses model declaration all of these models in the model's package will be utilized in other packages such as the handlers packages so make sure these structs are declared exported to be referenced by other packages lastly of course we'll have our main package as the entry point to kickstart our web application now notice that we have a simple and straightforward naming schemes for both of our packages and the files we have both of the main and non-main packages we have a minimum nesting also we need to make sure the packages is declared in these files that will match the name of the directories now the structure obviously would hugely depend on the project as well for example if the project is a tiny putting everything in the package main would not be the end of the world and if the project is on the larger side we can further separate things in the models package for example into multiple files and we can also add packages like persistence if we need to deal with a databases so this should help you get started to structure packages for your first few simple go projects if you want me to go over more complex project structuring using architecture design concepts like mvc domain driven the design then make sure to leave a comment down below i may be able to go through each one of these common designs specifically for gold projects as well if there's a demand for it so let me know for the time being head go to golangdojo.com to get your free golang qi sheet like and subscribe and i'll see you ninjas in the next video [Music] you
Info
Channel: Golang Dojo
Views: 536
Rating: undefined out of 5
Keywords: golang, golang 2021, learn golang, go lang, golang in 2021, go language, golang tutorial, go programming, golang for beginners, golang project structure explained, golang project, golang project structure, project structure in go, project structure in golang, golang packages, golang package structure explained, go package, go package structure, package structure in golang, golang package structure tutorial, go package tutorial, package structure in go, package in go
Id: 1MdX9Z9fWWw
Channel Id: undefined
Length: 7min 7sec (427 seconds)
Published: Fri Dec 17 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.