Accessing Go command line parameters

Accessing Go command line parameters

This is the first article of the mini series on how to provide options for Go applications. Today’s focus is on the default command line arguments/parameters/options, i.e., how to access Go command line parameters, and how to specify flags (command line parameters/options) to Go applications.

Just like C/C++, Go can access command line parameters. And no surprisingly at all the syntax is almost exactly as C. Here is an simple example:

package main

import (
        //      "fmt"
        "os"
        "path/filepath"
)

func main() {
        println("I am ", os.Args[0])

        baseName := filepath.Base(os.Args[0])
        println("The base name is ", baseName)

        // The length of array a can be discovered using the built-in function len
        println("Argument # is ", len(os.Args))

        // the first command line arguments
        if len(os.Args) > 1 {
                println("The first command line argument: ", os.Args[1])
        }
}

We can see that it can get/access the program name, the number of command line parameters, and each individual as well. Here is a sample test result, from compile to run:

$ go build CommandLineArgs.go

$ CommandLineArgs test one 
I am  CommandLineArgs
The base name is  CommandLineArgs
Argument # is  3
The first command line argument:  test

Although go is a compiling language, it can almost use-as/seem-like an interpreting language as well. Here is what happens when we run it without compiling first:

$ go run CommandLineArgs.go test one 
I am  /tmp/go-build829589774/command-line-arguments/_obj/exe/CommandLineArgs
The base name is  CommandLineArgs
Argument # is  3
The first command line argument:  test

We can see that Go compile it on the fly and the compiled executable is put under /tmp. The executable file name without the path (i.e., base name) is obtained by the built-in filepath.Base function.

That’s the end of today’s topic, the full source code is available here.

Advertisements

5 thoughts on “Accessing Go command line parameters

  1. Pingback: Passing options to Go from command line | SF-Xpt's Blog

  2. Pingback: TOML – Go De Facto config file | SF-Xpt's Blog

  3. Pingback: Beyond TOML, the Go’s De Facto config file | SF-Xpt's Blog

  4. Pingback: Providing Options for Go Applications | SF-Xpt's Blog

  5. Pingback: Showcasing the power of easygen with ffcvt | SF-Xpt's Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s