command-line-args

This commit is contained in:
Mark McGranaghan 2012-09-21 08:19:55 -07:00
parent e9744427aa
commit 918d09238a
3 changed files with 26 additions and 13 deletions

View File

@ -1,12 +0,0 @@
package main
import ("os"; "fmt")
func main() {
argsWithProgram := os.Args
argsWithoutProgram := os.Args[1:]
arg := os.Args[3]
fmt.Println(argsWithProgram)
fmt.Println(argsWithoutProgram)
fmt.Println(arg)
}

25
044-command-line-args.go Normal file
View File

@ -0,0 +1,25 @@
package main // Use `os.Args` to access command-line arguments and
// the name of the program.
import ("os"; "fmt")
func main() {
argsWithProg := os.Args // `os.Args` includes the program name as the first
argsWithoutProg := os.Args[1:] // value.
arg := os.Args[3] // `Args` are a slice, you can get individual args
// with normal indexing.
fmt.Println(argsWithProg)
fmt.Println(argsWithoutProg)
fmt.Println(arg)
}
/*
$ go build command-line-args // Build a `command-line-args` binary so that we have
// the expected program name.
$ ./command-line-args a b c d
[command-line-args a b c d]
[a b c d]
c
*/

View File

@ -41,7 +41,7 @@ select
timeout
string-fns
bytes
args
command-line-args
burstable-rate-limiting
dns
elapsed