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
+11.9k Golang : Determine if time variables have same calendar day
+13.6k Android Studio : Password input and reveal password example
+32.7k Golang : Regular Expression for alphanumeric and underscore
+10.5k Swift : Convert (cast) String to Integer
+10.6k Fix ERROR 1045 (28000): Access denied for user 'root'@'ip-address' (using password: YES)
+14.8k Golang : Find commonalities in two slices or arrays example
+8.9k Golang : GMail API create and send draft with simple upload attachment example
+36.7k Golang : Display float in 2 decimal points and rounding up or down
+17.2k Google Chrome : Your connection to website is encrypted with obsolete cryptography
+15.9k Golang : Get current time from the Internet time server(ntp) example
+10.6k Golang : Resolve domain name to IP4 and IP6 addresses.
+11.2k Golang : How to pipe input data to executing child process?