Created
May 5, 2024 07:42
-
-
Save gammazero/525fdffd273450edbcf8baadf849333a to your computer and use it in GitHub Desktop.
Golang multi-value flag
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"flag" | |
"strings" | |
) | |
type arrayFlags []string | |
func (a *arrayFlags) String() string { | |
return strings.Join(*a, ", ") | |
} | |
func (a *arrayFlags) Set(value string) error { | |
*a = append(*a, value) | |
return nil | |
} | |
func main() { | |
var serverURLs arrayFlags | |
flag.Var(&serverURLs, "serverURL", "URL of server to connect to. Multiple OK") | |
flag.Parse() | |
// ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment