go命令行参数

举例:打印name参数的值

package main

import (
  "flag"
  "fmt"
)
 
func main() {
  username := flag.String("name", "", "Input your username")
  flag.Parse()
  fmt.Println("Hello, ", *username)
}

 

编译:

➜  go_test go build flag.go

 

运行:如果没有指定参数值,打印第三个说明的参数"Input your username"


➜ go_test ./flag -name flag needs an argument: -name Usage of ./flag: -name string Input your username

  

如果指定参数值,打印结果

➜  go_test go run flag.go -name=welcome!
Hello,  welcome!

把-name的值赋给了username

猜你喜欢

转载自www.cnblogs.com/liurong07/p/10512250.html