This is a creation in Article, where the information may have evolved or changed.
We may need something like go get–u .... This way to implement our application, we cannot simply use flag. Parse, but to use Flagset, use the following example:
Package Main
Import (
"Flag"
"Log"
"OS"
)
var ()
Func Main () {
Read the command parameters, note that there are different parameters depending on the execution of the task.
Technical Reference:
Http://stackoverflow.com/questions/24504024/defining-independent-flagsets-in-golang
Http://play.golang.org/p/eaEEx_EReX
If Len (OS. Args) <= 1 {
Log. Println ("Please enter commands, supported commands: ESB, Web, host! ")
Os. Exit (-1)
}
Log. Println (OS. ARGS[1])
CMD: = os. ARGS[1]
ESB Client Tools
FESB: = flag. Newflagset ("Fesb", flag. ContinueOnError)
Consumer: = Fesb. Int ("C", 0, "The consumer's number is filtered in the ESB relationship, and the total is not lost.") ")
Provider: = Fesb. Int ("P", 0, "the number of the producer is filtered in the ESB relationship, and the output represents all.") ")
Dependtype: = Fesb. String ("T", "Cache", "type of ESB relationship dependency, default from cache on line, Optional: config (database configuration), cache (cached on-line call)")
Web site
Fweb: = flag. Newflagset ("Fweb", flag. ContinueOnError)
WebPort: = Fweb. Int ("P", "Bayi," the port number used by the WEB site. ")
Modify Host File Mode
Fhost: = flag. Newflagset ("Fhost", flag. ContinueOnError)
AAA: = Fhost. Bool ("B", False, "Test parameter!") ")
switch cmd {
case "ESB":
ERR: = Fesb. Parse (OS. Args[2:])
if err! = Nil {
log. PRINTLN ("ESB parameter type error,", err)
os. Exit ( -1)
}
log. Println ("Dependtype:", *dependtype)
log. Println ("Consumer:", *consumer)
log. Println ("Provider:", *provider)
Case "Web":
ERR: = Fweb. Parse (OS. Args[2:])
If err! = Nil {
Log. PRINTLN ("Web parameter type error,", err)
Os. Exit (-1)
}
Log. Println ("WebPort:", *webport)
Case "Host":
ERR: = Fhost. Parse (OS. Args[2:])
If err! = Nil {
Log. PRINTLN ("host parameter type error,", err)
Os. Exit (-1)
}
Log. Println ("AAA:", *AAA)
}
}
Resources:
Http://play.golang.org/p/eaEEx_EReX
Http://stackoverflow.com/questions/24504024/defining-independent-flagsets-in-golang
https://golang.org/pkg/flag/#NewFlagSet