Command-line arguments

The os.Args variable holds the command-line arguments, starting with the program name.

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

More code examples

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

Share this page: