举例:打印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编译