Command-Line Argument Handling in Go

Beginner

This tutorial is from open-source community. Access the source code

Introduction

The purpose of this lab is to practice working with command-line arguments in Golang.

Command-line arguments

The program currently prints out the raw command-line arguments passed to it. However, it needs to be modified to print out specific arguments based on their index.

  • Basic knowledge of Golang
  • Familiarity with command-line arguments
## To experiment with command-line arguments it's best to
## build a binary with `go build` first.
$ go build command-line-arguments.go
$ ./command-line-arguments a b c d
[./command-line-arguments a b c d]
[a b c d]
c

## Next we'll look at more advanced command-line processing
## with flags.

There is the full code below:

// [_Command-line arguments_](https://en.wikipedia.org/wiki/Command-line_interface#Arguments)
// are a common way to parameterize execution of programs.
// For example, `go run hello.go` uses `run` and
// `hello.go` arguments to the `go` program.

package main

import (
    "fmt"
    "os"
)

func main() {

    // `os.Args` provides access to raw command-line
    // arguments. Note that the first value in this slice
    // is the path to the program, and `os.Args[1:]`
    // holds the arguments to the program.
    argsWithProg := os.Args
    argsWithoutProg := os.Args[1:]

    // You can get individual args with normal indexing.
    arg := os.Args[3]

    fmt.Println(argsWithProg)
    fmt.Println(argsWithoutProg)
    fmt.Println(arg)
}

Summary

In this lab, we learned how to access and print out specific command-line arguments in Golang. By using the os.Args variable and indexing into it, we can easily access the arguments passed to the program.