I want to call a specific function based on a command line flag. The relevant parts of my code currently look like this:
package main
...
var enctool string
type encryptionFunction func(string, string) *os.File
func main() {
var ef encryptionFunction
// get encryption tool from flags, default to age
flag.StringVar(&enctool, "t", "age", "Encryption tool (age|gpg)")
flag.Parse()
// decide encryption backend
switch enctool {
case "age":
ef = encrypt.AgeEncrypt
case "gpg":
ef = encrypt.GPGEncrypt
default:
println("No encryption tool defined.")
return
}
// encrypt input file, output to tmpfile
tmpfile := ef(flag.Arg(0), recipient)
...
}
This works as I expect it to, but here’s the question:
Is this a legit way to handle this kind of flow or is there a more Go -appropriate approach, and if so what would be the benefit? Should I be looking at something like interfaces to handle this?
New contributor
MJH is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.