In the last video, we looked at setting up our Go project and getting all of the admin out of the way. However, the purpose of this course is to teach Go, so in this video, we’ll actually be doing a little bit of programming and creating the entrypoint for our application.
Our Application Entrypoint
Let’s create a new main.go
file. Now this file is going to live under a cmd/cli/
directory. The reason for this is that, when building larger Go projects, you may want to expose your application as a REST API, or as a CLI or as a client app. For each of these examples, we want the entrypoint of our application to live within a dedicated directory under the cmd/
directory.
It’s important to note that whilst each of these directories will have it’s own main.go
file, these files must only perform the action of startup and teardown of our application.
For example, if we were running a web server, we would want this file to initialize all of the components of our application and spin up a webserver using these components and then handle the graceful shutdown of our application.
For our CLI, we only want it parsing the incoming command and acting as a kind of router to the internal logic which will be housed within a different package/different directory entirely.
Our Starting Code
Let’s add some code to this newly created file now. For now, we’ll just keep it as a nice and lightweight Go app which just prints out Network CLI
:
package main
import "fmt"
func main() {
fmt.Println("Network CLI")
}
We can then try running this from the root of our project like so:
Network CLI
Perfect, we now have a starting point from which we can build upon.
Building
In production environments, whether it be for a CLI, or a server, your application will be run as a standalone binary executable.
Let’s now take a look at how we can build this application as a binary executable:
OUTPUT FROM BUILD
You should see a new cli
executable appear in the root directory of your project. We can then attempt to run this like we would any binary executable:
Note - You may have to set the execution bit on this executable file. For *Nix systems you can do that by typing:
chmod +x cli
.
Network CLI
Note - If you are on Windows, you may have to set the -o flag to
cli.exe
and run./cli.exe
in order for this to work.
Awesome, we have been able to compile our application as a binary executable and run it locally!
Installing Locally
The go
tool can also handle the installing of binaries onto your local machine’s path for you. We can run go install
in order to compile our application’s binary and then move it to a specific path which our local machine should already be setup to search:
OUTPUT FROM INSTALL
We should now be able to run our cli
command from any directory on our machine. Test this now by going up 1 directory and running ./cli
and it should output Network CLI
.
Conclusion
Perfect, in this video, we’ve created our application’s entrypoint and looked at how we can run our application using the go run
command. We then looked at how we can take this a step further and compile our application as a binary and how we can use the go install
command to also do the step of installing our application to our local machine’s PATH.