Before we start building our CLI application, you need to have the following:
GOPATH
and PATH
environment variables correctly.mkdir mycli
cd mycli
go mod init github.com/yourusername/mycli
main.go
file in the project directory.touch main.go
Go has a built - in flag
package that makes it easy to parse command - line arguments. Here is a simple example:
package main
import (
"flag"
"fmt"
)
func main() {
// Define a flag for a string argument
name := flag.String("name", "World", "Your name")
// Parse the command - line arguments
flag.Parse()
fmt.Printf("Hello, %s!\n", *name)
}
In this example, we define a name
flag using flag.String
. The first argument is the flag name, the second is the default value, and the third is the help message. After defining the flags, we call flag.Parse()
to parse the command - line arguments.
To run this program, save the code in main.go
and run the following command:
go run main.go -name=John
The output will be:
Hello, John!
For more complex CLI applications, we often need to support multiple commands. We can use the cobra
library, which is a popular choice for building CLI applications in Go.
go get -u github.com/spf13/cobra/cobra
cobra init mycli --pkg-name github.com/yourusername/mycli
cobra add greet
cmd/greet.go
file and modify it as follows:package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
var name string
// greetCmd represents the greet command
var greetCmd = &cobra.Command{
Use: "greet",
Short: "Greet someone",
Long: `Greet someone by their name.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("Hello, %s!\n", name)
},
}
func init() {
rootCmd.AddCommand(greetCmd)
greetCmd.Flags().StringVarP(&name, "name", "n", "World", "Your name")
}
In this example, we define a new command greet
using cobra.Command
. We also define a flag name
for the command.
To run the greet
command, use the following command:
go run main.go greet -n=Jane
The output will be:
Hello, Jane!
The cobra
library automatically generates help and usage information for your commands. When you run the following command:
go run main.go --help
You will see a list of available commands and flags along with their descriptions.
Proper error handling is crucial in CLI applications. In Go, we typically return errors from functions and handle them at the call site. Here is an example:
package main
import (
"fmt"
"os"
)
func readFile(filename string) ([]byte, error) {
data, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
return data, nil
}
func main() {
filename := "test.txt"
data, err := readFile(filename)
if err != nil {
fmt.Printf("Error reading file: %v\n", err)
os.Exit(1)
}
fmt.Printf("File content: %s\n", string(data))
}
In this example, the readFile
function returns an error if there is a problem reading the file. In the main
function, we check for the error and handle it appropriately.
Building a CLI application in Golang can be a straightforward process, thanks to the standard library and powerful third - party libraries like cobra
. By following the steps outlined in this guide, you can create simple to complex CLI applications with ease. Remember to follow the best practices to make your CLI application robust and user - friendly.