GO-BASH-MAGIC
INTRODUCTION
For the love of Linux, VSCode, and Bash Scripting, today we are bashing in the VSCodes bash terminal. In the other articles, we are entirely going to install and enjoy a certain Linux step-by-step, not in one piece but in a few articles. Don’t forget, breaks are essential.
NOTE!
We are not diving into that, we are creating a script that creates a Go Web API folder structure.
Most cutting-edge technology happens on Linux, from most of the internet to the world’s fastest supercomputers and as said before, even the cloud itself.
WHAT WE ARE BUILDING?
We are creating a bash script that will set up a Golang API generator with its relevant folders, from Routes, Responses, and Controllers, and lastly, we make a file named server.go ti’s will be handled by the same script.
Our server.go file is where we write our primary(main) entry function to execute the program.
TO CLONE THE REPOSITORY FROM GITHUB
LET’S GO BASH
Let’s create a file with a .sh extension, I’m naming mine runner.sh. All our scripts will be written in this file then we will break it down for a better understanding.
NOW! LET’S BREAK DOWN THE PROCESSES
Importance of the First Line in our file
#!/bin/bash
We have the shebang, #!/bin/bash when used in scripts is used to instruct the operating system to use bash as a command interpreter. Each of the systems has its own shells which the system will use to execute its own system scripts. This system shell can vary from OS to OS(most of the time it will be bash).
Line 2 is a comment, quite similar to some comments in other Programming Languages.
this is just explaining what we are going to create in this article.
Lines 4–11, we are creating our function, we are calling it create_server_file, the main file, and an import for the created file.
Inside the block we have the following lines of instructions that will create and append to the file we are calling server.go
create_server_file() {
printf "package $1\n" >> server.go
printf "\n" >> server.go
printf "$2 "'("fmt")'"\n" >> server.go
printf "\n" >> server.go
printf "\n" >> server.go
printf ""'func main(){\n}'"\n" >> server.go
}
In lines 14–19, we are starting with creating an array of the folders we want to create for our project, we are calling create_server_file passing in Two arguments, the main file and an import for the created file.
Lastly, we have our for loop which is handling the creation of our files in our array of folders, after iterating through the array.
Then we create all our folders using the loop, we have a do keyword that handles creation when complete the done keyword ends the loop.
I strongly believe that by now, you can create any script and automate your daily tasks.
list='Responses Contollers Model'
create_server_file main import
for var in $list
do
mkdir $var
done
CONCLUSION
Thank you for reaching this far to easily create a GOlang API folder structure, using BASH.