Go: 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")
                return
        }
        pattern := os.Args[1]
        file := os.Args[2]
        // ...
}
$ go build grep.go
$ ./grep
Usage: ./grep PATTERN FILE

Comments

Be the first to comment!