1. 程式人生 > >Go flag包示例

Go flag包示例

Go 命令列解析示例

func test_flag() {
    ListPort := flag.String("list", "1025", "list port")
    Server := flag.String("server", "192.168.1.134:9000", "server address")
    MonitorServer := flag.String("monitor", "192.168.1.134:9002", "Monitor server address")
    PQPS := flag.Uint("qps", 100, "QPS")

    PMinUserId := flag.Uint
("min", 0, "MinUserId") PMaxUserId := flag.Uint("max", 99, "MaxUserId") PTotalUser := flag.Uint("total", 100, "TotalUser") flag.Parse() //注意, 如果想獲取命令列的指定值, 必須要寫入該行 fmt.Println(*ListPort, *Server, *MonitorServer, *PQPS) fmt.Println(*PMinUserId, *PMaxUserId, *PTotalUser) } func main(){ test_flag() } 編譯生成 ./server.exe
程式 //輸出: ./server.exe -list 5000 -server 9000 -monitor 9000 5000 9000 9000 100 0 99 100 ./server.exe 1025 192.168.1.134:9000 192.168.1.134:9002 100 0 99 100