The os package and flag package are used to read the main command input
- 2020-05-27 05:52:22
- OfStack
The Args package includes the command name itself as the first value of the read parameter.
flag.Args reads the input after the command (to be used with flag.Parse ()).
That is, os has one more command name read than flag.
package main
import (
"flag"
"fmt"
"os"
)
func main() {
// os.Args way
args := os.Args
if args == nil || len(args) < 2 {
fmt.Println("Hello The world !")
} else {
fmt.Println("Hello ", args[1]) // The first 2 Number of parameters, number 1 Three arguments are the command names
}
// flag.Args way
flag.Parse()
var ch []string = flag.Args()
if ch != nil && len(ch) > 0 {
fmt.Println("Hello ", ch[0]) // The first 1 Start with
}
}
That's all for this article, I hope you enjoy it.