Why Packages in go programming language Part 2
Packages In Golang
Packages in golang are used to organize our code. Is used to reuse our code. Same as dll we refer in C# and call the classes or create an object of classes which are in that namespace present in that dll. When you declared package then you can easily import that package where you need to use that.
When you use a package that is created by the third party you need to give a fully qualified package name. It like one folder contains many files. If you want all files to include in one package then whatever file you give need to add same package name in that files.
By using the package you will able to access functions from one file to another file. This import has file scope. The ultimate meaning of this is function imported or exported like angular you will able to access the function from one class to another class by using export keyword but in go language only you need to import that package.
In this go language world means you have a folder and thatfolder contains one or more go programming files then every file in that folderhas package name same as a folder name and try to use all small caps letter forfolder/ package name. The folder which contains that go files is ultimatelypackage.
In the go language, there is no public, private, protected like access modifier like other programming languages like java, c#, it only has visible or not visible.
Capitalization:in this, if you want to export function means you want your function should bevisible to outside of that package you need to capitalize that name of the function.
Lowercase: it means you don’t want to export it is not visible outside of package if you define a function with lower case.
Mostly Used Golang Commands
Go run the command
This command will compile and run main package which contains .go files which are specified on the command line as an argument. This command has compiled that code to temporary folder.
D:\Target Dec-2019\GoProgramming\GoLanguageLab>go run go run: no go files listed D:\Target Dec-2019\GoProgramming\GoLanguageLab>go run main.go ragas
Go Build Command
The go build command compiles the package named by theimport path and their dependencies. It will create executable of that projectwhen you give the main file name as argument which resides in the samedirectory of that project. If you provide a list of .go files from a singledirectory the go compiler treats them as a list of source files specifyingpackage.
D:\Target Dec-2019\GoProgramming\GoLanguageLab>go build
If you give other files names to go build command other than the main file it will give no main file then it doesn’t do anything.
Go Run Command
This command will compile and runs the main package whichcomprised of the .go files.
If you fire go run command on a package that doesn’t contain main method file and no file name provides like the first command it will throw such kind of message.
In the second case if you provide a filename to go run command and the file doesn’t contain the main function it will throw the second message shown in the above image.
D:\Target Dec-2019\GoProgramming\GoLanguageLab\utilitypackage>go run go run: no go files listed D:\Target Dec-2019\GoProgramming\GoLanguageLab\utilitypackage>go run string.go go run: cannot run non-main package D:\Target Dec-2019\GoProgramming\GoLanguageLab\utilitypackage>
Go Clean Command
The go command builds most of the objects in a temporary directory. When you fire goes clean command it will remove that object created in the temporary directory also if your code contains some executable created by command it will remove that also.
D:\Target Dec-2019\GoProgramming\GoLanguageLab>go clean
GitHub Project Link :- https://github.com/Sagar-Jaybhay/GoLanguageLab