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?