Go is a powerful and modern programming language that can be used to create high-performance command-line applications. With its simple syntax, powerful concurrency features, and built-in support for compiling to standalone executables, Go is an excellent choice for building tools that can be distributed and run on any platform. In this tutorial, we'll walk you through the process of creating a simple command-line application using Go.


Prerequisites

Before you get started, you'll need to have Go installed on your computer. You can download it from the official website (https://golang.org/dl/).


Creating a new Go module

First, we need to create a new Go module. Open a terminal window and create a new directory for your project:

$ mkdir mycli
$ cd mycli

Next, we'll create a new Go module using the go mod init command:

$ go mod init mycli

This command will create a new go.mod file in your project directory. The go.mod file is used to manage dependencies for your project.


Creating a new Go file

Next, we'll create a new Go file in our project directory. We'll call this file main.go. Open your favorite text editor and create a new file named main.go in the mycli directory:

$ touch main.go

In your text editor, add the following code to the main.go file:

package main

import "fmt"

func main() {
    fmt.Println("Hello, world!")
}

This code defines a simple Go program that will print "Hello, world!" to the console when it's run.


Building and running the program

Now that we've created our Go program, we can build it and run it from the command line. To build the program, use the go build command:

$ go build

This command will compile your program and create an executable file in your project directory. The executable file will be named mycli (the same name as your project directory).

To run the program, simply type its name:

$ ./mycli
Hello, world!

Congratulations, you've just created and run your first Go program!


Adding command-line arguments

Now that we have a basic command-line application, let's add support for command-line arguments. Command-line arguments allow users to pass additional information to the program when it's run.

In our main.go file, let's modify the main() function to accept a string argument:

package main

import (
    "fmt"
    "os"
)

func main() {
    if len(os.Args) != 2 {
        fmt.Println("Usage: mycli [name]")
        os.Exit(1)
    }

    name := os.Args[1]
    fmt.Printf("Hello, %s!\n", name)
}

In this code, we're using the os.Args variable to access the command-line arguments passed to our program. The first argument (os.Args[0]) is always the name of the program itself. In this case, we're expecting a second argument (os.Args[1]) which will be the name of the person we want to greet.

The if statement checks whether there are exactly two command-line arguments. If there are not, it prints a usage message and exits the program with a non-zero exit code.

The name variable is set to the second command-line argument (os.Args[1]). We then use fmt.Printf() to print a personalized greeting to the console.


Building and running the program with command-line arguments

Now that we've added support for command-line arguments, let's build and run the program again. Use the go build command to compile the program:

$ go build

Then, run the program with a command-line argument:

$ ./mycli Alice
Hello, Alice!

You should see a personalized greeting with the name you provided as the command-line argument.


Conclusion

In this tutorial, we've shown you how to create a simple command-line application using Go. We've covered the basics of Go programming, including creating a new module, creating a new Go file, building and running the program, and adding support for command-line arguments. With this knowledge, you can now start building your own command-line applications using Go. Happy coding!