Package os provides Exit() and access to file I/O,
command-line arguments, etc. (Flag package
appears shortly.)
Background info:
$godoc -src os Args
var Args []string // provided by runtime
func os.Exit(code int)
Exit causes the current program to exit with the given status code. Conventionally, code zero indicates success, non-zero an error.
Discussion:
if len(os.Args) < 2 { // len(os.Args) is the length of the runtime provided array of command line arguments.
for i := 1; i < len(os.Args); i++ {
fmt.Printf("arg %d: %s\n", i, os.Args[i])
}
for initialization; condition; post suite {suite}
at initialization i = 1, so output is arg 1: firstcommandlineargument
os.Args[i] is an array that's being accessed as a slice? so, here it must be os.Args[1], because initialization i := 1, so it's the second slot of os.Args?
changing:
for i := 0; i < len(os.Args); i++ {
$6g echo.go
$6l echo.6
./6.out one two three
arg 0: ./6.out
arg 1: one
arg 2: two
arg 3: three
so, yes, it looks like it's accessing the second slot of os.Args, and os.Args[0] (what I'm calling slot 1) looks like it's the the file calling the command.
No comments:
Post a Comment