更新 2021-9-26
我发现这是一个愚蠢的问题。
这是因为标志将在调用标志后更新。解析()。
nReq := *flag_var
flag.Parse() // flag_var update
fmt.Println(nReq) // nReq is unchanged.
因此,最佳做法是使用标志。取而代之的是 IntVar(),我们可以键入更少的字符。
为什么我不能像这样使用返回值的点类型?
// test.go
nReq := *flag.Int("n", 10000, "set total requests")
flag.Parse()
fmt.Println(nReq)
// test -n 200
10000
// the value is still 10000.
它始终返回默认值 (10000)。
我需要使用:
nReq := flag.Int("n", 10000, "set total requests")
flag.Parse()
fmt.Println(*nReq)
// test -n 200
200
// the value is updated to the new flag(200)
冉冉说
MMTTMM
随时随地看视频慕课网APP
相关分类