Reading Command Line Arguments in Go: An End-to-End Example

Go is a programming language that is gaining popularity for its simplicity and efficiency. One of the things that make Go so easy to use is its built-in support for command-line arguments. In this article, we will walk through an end-to-end example of how to read command line arguments in Go.

Getting Started

First, let's create a new Go file to work with. Open up your favorite text editor and create a new file called main.go. In this file, we will create a simple program that takes a command line argument and prints it out to the console.

package main

import (
    "fmt"
    "os"
)

func main() {
    if len(os.Args) > 1 {
        fmt.Println("Hello, " + os.Args[1])
    } else {
        fmt.Println("Hello, World!")
    }
}

In this program, we are using the os package to access the command line arguments passed to the program. We are also using the fmt package to print out the result to the console.

Running the Program

To run this program, navigate to the directory where the main.go file is saved and executes the following command:

go run main.go John

This will print out the message "Hello, John" to the console. If you don't pass a command line argument, the program will default to printing out "Hello, World!".

Parsing Command Line Flags

While reading command line arguments is easy enough with the os package, parsing command line flags can be a bit more involved. Fortunately, Go provides a built-in flag the package that makes it easy to parse command line flags.

Let's modify our previous program to take a command line flag instead of a command line argument. Open up main.go and replace the contents with the following code:

package main

import (
    "flag"
    "fmt"
)

func main() {
    name := flag.String("name", "World", "a string")
    flag.Parse()
    fmt.Printf("Hello, %s!\n", *name)
}

In this program, we are using the flag package to define a new flag called name. The flag.String function takes three arguments: the name of the flag, the default value of the flag, and a description of the flag. We are then calling the flag.Parse function to parse the command line flags passed to the program.

Running the Program

To run this program, navigate to the directory where the main.go file is saved and execute the following command:

go run main.go -name John

This will print out the message "Hello, John!" to the console. If you don't pass a command line flag, the program will default to printing out "Hello, World!".

Conclusion

In this article, we walked through an end-to-end example of how to read and parse command line arguments in Go. While reading command line arguments is easy enough with the os package, parsing command line flags can be a bit more involved. Fortunately, Go provides a built-in flag package that makes it easy to parse command line flags. With these tools, you can create powerful command line programs in Go that are both easy to use and easy to maintain.

I hope this helps, you!!

More such articles:

https://medium.com/techwasti

https://www.youtube.com/channel/UCiTaHm1AYqMS4F4L9zyO7qA

https://www.techwasti.com/

\==========================**=========================

If this article adds any value to you then please clap and comment.

Let’s connect on Stackoverflow, LinkedIn, & Twitter.

Did you find this article valuable?

Support techwasti by becoming a sponsor. Any amount is appreciated!