調用flag包能夠方便的獲取到命令行中傳遞的參數,好比能夠實現相似nginx執行程序獲取命令行參數執行不一樣操做的目標nginx
package main import ( "flag" "fmt" "os" ) func main() { var h bool var c, s string flag.BoolVar(&h, "h", false, "幫助信息") flag.StringVar(&c, "c", "conf/nginx.conf", "設置配置文件:") flag.StringVar(&s, "s", "", "管理服務:reload|restart|start|stop") flag.Parse() if h { fmt.Fprintf(os.Stdout, "nginx version: nginx/%s\n", "100.0.0") flag.PrintDefaults() } if c != "" { fmt.Println("-c參數是", c) } if s != "" { fmt.Println("-s參數是", s) } }