Previous: , Up: Tutorial   [Index]


1.2 Command-Line Arguments

os.Args Variable

The os package provides functions and other values for dealing with the operating system in a platform-independent fashion.

Command-line arguments are available to a program in a variable named Args that is part of the os package; thus its name anywhere outside the os package is os.Args.

Slices

The variable os.Args is a slice of strings. Slices are a fundamental notion in Go, and we’ll talk a lot more about them soon. For now, think of a slice as

Elements of Slice

  1. The first element of os.Args, os.Args[0], is the name of the command itself;
  2. the other elements are the arguments that were presented to the program when it started execution.
  3. A slice expression of the form s[m:n] yields a slice that refers to elements ‘m’ through ‘n-1’, so the elements we need for our next example are those in the slice os.Args[1:len(os.Args)]. If ‘m’ or ‘n’ is omitted, it defaults to 0 or len(s) respectively, so we can abbreviate the desired slice as os.Args[1:].

Previous: Hello World, Up: Tutorial   [Index]