read input argument in go

In Go, we can read input arguments using the standard library "flag".

Here's an example on how to read and parse input arguments in Go using "flag":

main.go
package main

import (
    "flag"
    "fmt"
)

func main() {
    // Define the arguments and their default values
    path := flag.String("path", "", "the path of the file to read")
    count := flag.Int("count", 10, "number of lines to read")

    // Parse the arguments
    flag.Parse()

    // Access the values
    fmt.Printf("Reading %d lines from file %q\n", *count, *path)
}
382 chars
19 lines

You can run the program with the following command:

main.go
go run main.go --path=/path/to/file --count=20
47 chars
2 lines

This will parse the input arguments and print the message:

main.go
Reading 20 lines from file "/path/to/file"
43 chars
2 lines

related categories

gistlibby LogSnag