Golang flag.FlagSet.Var() function example
package flag
Var defines a flag with the specified name(2nd parameter) and usage string(3rd parameter). The type and value of the flag are represented by the first argument, of type Value (1st parameter), which typically holds a user-defined implementation of Value. For instance, the caller could create a flag that turns a comma-separated string into a slice of strings by giving the slice the methods of Value; in particular, Set would decompose the comma-separated string into the slice.
Golang flag.FlagSet.Var() function usage example
package main
import (
"flag"
"fmt"
)
type flagStr struct {
value *string
}
func (f *flagStr) Set(value string) error {
f.value = &value
return nil
}
func (f *flagStr) Get() *string {
return f.value
}
func (f *flagStr) String() string {
if f.value != nil {
return *f.value
}
return ""
}
var (
flagSet *flag.FlagSet
appFlags struct {
appID flagStr
appSecret flagStr
}
)
func main() {
flagSet = flag.NewFlagSet("example", flag.ContinueOnError)
flagSet.Var(&appFlags.appID, "FBAppID", "Facebook Application ID") // <-- here
appFlags.appID.Set("1234567890")
err := flagSet.Parse([]string{"FBAppID"})
if err != nil {
fmt.Println(err)
}
fmt.Println(flagSet.Lookup("FBAppID"))
fmt.Println(&appFlags.appID)
}
Output :
&{FBAppID Facebook Application ID 1234567890 }
1234567890
Reference :
Advertisement
Something interesting
Tutorials
+5.2k JavaScript/JQuery : Redirect page examples
+34.1k Golang : Create x509 certificate, private and public keys
+7.7k Golang : Command line ticker to show work in progress
+22.7k Golang : Strings to lowercase and uppercase example
+9.3k Golang : Generate random Chinese, Japanese, Korean and other runes
+5.2k Python : Create Whois client or function example
+7.3k Golang : How to convert strange string to JSON with json.MarshalIndent
+7.4k Golang : Example of custom handler for Gorilla's Path usage.
+6.5k Golang : Combine slices of complex numbers and operation example
+10.7k Golang : Underscore string example
+5.4k Golang : What is StructTag and how to get StructTag's value?
+9.4k Facebook : Getting the friends list with PHP return JSON format