9

Command-line arguments and flags

 3 years ago
source link: https://yourbasic.org/golang/command-line-arguments/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Command-line arguments and flags

yourbasic.org/golang
tibetan-prayer-flags.jpg

The os.Args variable holds the command-line arguments – starting with the program name – which are passed to a Go program.

func main() {
    if len(os.Args) != 3 {
        fmt.Println("Usage:", os.Args[0], "PATTERN", "FILE")
        return
    }
    pattern := os.Args[1]
    file := os.Args[2]
    // ...
}
$ go build grep.go
$ ./grep
Usage: ./grep PATTERN FILE

Flag parsing

The flag package implements basic command-line flag parsing.

More code examples

Go blueprints: code for com­mon tasks is a collection of handy code examples.

Share:             


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK