作者:呼呼小琰琰 | 来源:互联网 | 2023-09-24 13:18
命令行参数个数调用os包即可。获取参数个数,遍历参数packagemainimport(fmtos)funcmain(){fmt.Printf(Numberofargsi
命令行参数个数
调用 os
包即可。
获取参数个数, 遍历参数
package main
import (
"fmt"
"os"
)
func main() {
fmt.Printf("Number of args is %d\n\n", len(os.Args))
for _, arg := range os.Args {
fmt.Println(arg)
}
}
命令行参数解析和设置
调用 flag
包即可。
例子
设置参数
package main
import (
"flag"
"fmt"
)
var (
name = flag.String("name", "Tom", "Please input your name:")
age = flag.Int("age", 6, "Please input your age:")
hasMoney = flag.Bool("hasMoney", true, "Do you have any money?")
)
func main() {
flag.PrintDefaults()
flag.Parse()
fmt.Printf("name is %s\n", *name)
fmt.Printf("name is %d\n", *age)
fmt.Printf("name is %t\n", *hasMoney)
}
联系我
go