Creating Your First CLI App in Golang

The purpose of this blog is to help those who are new to Go Programming Language and wants to create an application or have never worked on creating a CLI application before.
Though this will be a very small step towards learning Go applications, it will be a potentially useful one.

So let’s start.

Let's consider a scenario, where you have a log file. A standard logfile format has basically 3 fields Timestamp and Log Level followed by a message of some sort.

Just imagine, in a real-world application there are hundreds or thousands of logs of such kind and you are tasked to find a way to organize this log file and fetch logs that are specifically indicating errors that occurred in the application.

So to solve this problem, we will create a Go Application which will go through all the logs in the log file and will separate out logs with ERROR log level.

So let’s start with creating a Go Application.

Creating a Go CLI App

Create a Project Directory, say Golang-First-CLI-App.

$ mkdir Golang-First-CLI-App
$ cd Golang-First-CLI-App

While inside our new project directory, we’re now going to initialize our project as a Go module. This means anybody will be able to install our Go code off Github if they so choose. I know I’m going to save my repo to https://github.com/sagar23sj/Golang-First-CLI-App, so we run the following:

# Creating a Go Module

A new file will now appear in your project directory called go.mod.
go.mod file contains information about our module for others. As we install dependencies for our project, these dependencies, and their respective versions will be stored here.

Create Source File : main.go

All source files start with a package declaration, so we will go ahead and declare package main. Now in order for go to figure out the entry point for our application, we need to have a well-known entry point. So we will create the main function here.

# main.go

Now from other programming languages, you might have been used to seeing command line parameter as first parameter to main function which accepts command line parameters passed as the application starts up.

Now, Go also has access to command line parameters, but it is not necessary to deal with them every time.

Now, we will start constructing our application.

package main

Output :

$ go run main.go 

Now, is this what we wanted?? Partially, yes. This is just half the part, which is filtering.

Now, In the current implementation, we have a fixed location of log file and fixed log-level, but moving forward what we want is user must be able to specify the location of the log file and also the log-level we are reading.

Golang generally doesn't force us to use command line parameters, but if we do want to use it. We can use golang package called flag.

flag package implements command-line flag parsing.

Let’s now look at the code, where to add what to accept arguments from the command line.

package main

Now, we can check 3 outputs in this case

Output 1: go run main.go

$ go run main.go

This is similar to earlier since we have set default values for our program.

Output 2: go run main.go -help

$ go run main.go -help

When we pass argument -help, it will display the help document on command line that we mentioned in 3rd argument to the flag variables.

Output 3: go run main.go -level FATAL

$ go run main.go -level FATAL
2012-02-03 18:35:34 SampleClass4 [FATAL] system problem at id 1991281254

For this output to be fetched, we passed the command-line argument to our program as -level FATAL.

Seems like what we wanted from the start i.e to able to create a Go Application which accepts command-line arguments and does the operations based on that.

So that’s as simple as it is.

Now obviously, there is more to command-line applications with GO. The entire standard library is available so you can experiment.

I hope this gives you a taste of what it’s like to create a simple command-line application in Go.

Thank You.

Github-Link to the code : https://github.com/sagar23sj/Golang-First-CLI-App

Software Engineer @Josh Software Pvt. Ltd.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store