英文:
Golang execute function using flags
问题
你好!根据你提供的代码,当你执行最后一个标志时,所有的函数都会被执行。这是因为在你的代码中,你没有对每个标志进行单独的处理,而是在主函数中使用了三个条件判断来执行相应的函数。
在你的代码中,当执行最后一个标志时,forkPtr
变量的值为true
,因此会执行fork(forkPtr)
函数。同时,由于wordPtr
和numbPtr
的值并没有被修改,它们的初始值仍然有效,所以word(wordPtr)
和numb(numbPtr)
函数也会被执行。
如果你只想执行特定的函数,你可以在主函数中使用if-else
语句来判断执行哪个函数。例如,你可以修改代码如下:
func main() {
flag.StringVar(&wordPtr, "word", "foo", "a string")
flag.IntVar(&numbPtr, "numb", 42, "an int")
flag.BoolVar(&forkPtr, "fork", false, "a bool")
flag.Parse()
if flag.Arg(0) == "-wordPtr" {
word(wordPtr)
} else if flag.Arg(0) == "-numbPtr" {
numb(numbPtr)
} else if flag.Arg(0) == "-forkPtr" {
fork(forkPtr)
}
}
这样修改后,只有与输入的标志匹配的函数会被执行。希望这可以帮助到你!如果还有其他问题,请随时提问。
英文:
How can I execute a specific function by entering a flag, for example, I have three flags:
wordPtr
numbPtr
forkPtr
And when executing a single one, the function that calls is executed:
.\data -wordPtr Test
When executing that flag, only the word function is executed:
package main
import (
"flag"
"fmt"
)
var (
wordPtr string
numbPtr int
forkPtr bool
)
func main() {
flag.StringVar(&wordPtr, "word", "foo", "a string")
flag.IntVar(&numbPtr, "numb", 42, "an int")
flag.BoolVar(&forkPtr, "fork", false, "a bool")
flag.Parse()
if wordPtr != `` {
word(wordPtr)
}
if numbPtr != 0 {
numb(numbPtr)
}
if forkPtr {
fork(forkPtr)
}
}
func word(word string) {
fmt.Println("word:", word)
}
func numb(numb int) {
fmt.Println("numb:", numb)
}
func fork(fork bool) {
fmt.Println("fork:", fork)
}
But, when I execute the last flag, all functions are executed
PS C:\golang> .\logdata.exe -fork
word: foo
numb: 42
fork: true
PS C:\golang>
Do you know why?
答案1
得分: 1
你为word
和numb
标志提供了默认值,这意味着如果你没有将它们作为命令行参数提供,它们将使用默认值。
在flag.Parse()
之后,你只测试wordPtr
不为空或numbPtr
不为0
,由于默认值不是这些值,测试将通过并执行函数。
如果你将空字符串和0
作为默认值:
flag.StringVar(&wordPtr, "word", "", "a string")
flag.IntVar(&numbPtr, "numb", 0, "an int")
那么如果你没有将它们作为命令行参数提供,测试将不会通过:
.\logdata.exe -fork
将输出:
fork: true
如果你想测试是否提供了word
或numb
作为命令行参数,请参考https://stackoverflow.com/questions/35809252/check-if-flag-was-provided-in-go。
英文:
You provide default values for the word
and numb
flags, which means if you don't provide them as cli arguments, they will get the default value.
And after flag.Parse()
you only test if wordPtr
is not empty or numbPtr
is not 0
, and since the default values are not these, the tests will pass and the functions will be executed.
If you use the empty string and 0
as the defaults:
flag.StringVar(&wordPtr, "word", "", "a string")
flag.IntVar(&numbPtr, "numb", 0, "an int")
Then if you don't provide these as cli arguments, your tests will not pass:
.\logdata.exe -fork
Will output:
fork: true
If you want to test if word
or numb
was provided as cli arguments, see https://stackoverflow.com/questions/35809252/check-if-flag-was-provided-in-go.
答案2
得分: 0
根据https://pkg.go.dev/flag#StringVar
StringVar定义了一个具有指定名称、默认值和用法说明的字符串标志。参数p指向一个字符串变量,用于存储标志的值。
所以在你的情况下,"foo"
将是wordPtr
的默认值,42
将是numbPtr
的默认值,false
将是forkPtr
的默认值。
英文:
According to https://pkg.go.dev/flag#StringVar
> StringVar defines a string flag with specified name, default value, and usage string. The argument p points to a string variable in which to store the value of the flag.
So it means in your case "foo"
- will be default value for wordPtr
, 42
for numbPtr
and false
for forkPtr
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论