舉例:打印name參數的值ui
package main import ( "flag" "fmt" ) func main() { username := flag.String("name", "", "Input your username") flag.Parse() fmt.Println("Hello, ", *username) }
編譯:code
➜ go_test go build flag.go
運行:若是沒有指定參數值,打印第三個說明的參數"Input your username"blog
➜ go_test ./flag -name flag needs an argument: -name Usage of ./flag: -name string Input your username
若是指定參數值,打印結果string
➜ go_test go run flag.go -name=welcome! Hello, welcome!
把-name的值賦給了username編譯