lean into examples
This commit is contained in:
23
examples/command-line-arguments/command-line-arguments.go
Normal file
23
examples/command-line-arguments/command-line-arguments.go
Normal file
@@ -0,0 +1,23 @@
|
||||
// Use `os.Args` to access command-line arguments and
|
||||
// the name of the program.
|
||||
package main
|
||||
|
||||
import "os"
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
// `os.Args` includes the program name as the first
|
||||
// value.
|
||||
argsWithProg := os.Args
|
||||
argsWithoutProg := os.Args[1:]
|
||||
|
||||
// `Args` are a slice, you can get individual args
|
||||
// with normal indexing.
|
||||
arg := os.Args[3]
|
||||
|
||||
fmt.Println(argsWithProg)
|
||||
fmt.Println(argsWithoutProg)
|
||||
fmt.Println(arg)
|
||||
}
|
||||
|
||||
// todo: discuss building before here
|
||||
@@ -0,0 +1,9 @@
|
||||
# Build a `command-line-args` binary so that we have
|
||||
# the expected program name.
|
||||
$ go build command-line-arguments
|
||||
|
||||
|
||||
$ ./command-line-arguments a b c d
|
||||
[command-line-arguments a b c d]
|
||||
[a b c d]
|
||||
c
|
||||
Reference in New Issue
Block a user