command-line-args
This commit is contained in:
parent
e9744427aa
commit
918d09238a
12
044-args.go
12
044-args.go
@ -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
25
044-command-line-args.go
Normal 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
|
||||
*/
|
@ -41,7 +41,7 @@ select
|
||||
timeout
|
||||
string-fns
|
||||
bytes
|
||||
args
|
||||
command-line-args
|
||||
burstable-rate-limiting
|
||||
dns
|
||||
elapsed
|
||||
|
Loading…
x
Reference in New Issue
Block a user