Golang flag.Var() function example
package flag
Var defines a flag with the specified name(2nd parameter) and usage string(3rd parameter). The type and value(1st parameter) of the flag are represented by the first argument, of type Value, 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.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 (
appFlags struct {
appID flagStr
appSecret flagStr
}
)
func main() {
flag.Var(&appFlags.appID, "FBAppID", "Facebook Application ID")
appFlags.appID.Set("1234567890")
flag.Var(&appFlags.appSecret, "FBAppSecret", "Facebook Application Secret")
flag.Parse()
fmt.Println(flag.Lookup("FBAppID")) // print the Flag struct
fmt.Println(&appFlags.appID)
}
Output :
&{FBAppID Facebook Application ID 1234567890 }
1234567890
Reference :
Advertisement
Something interesting
Tutorials
+6.4k CodeIgniter : form input set_value cause " to become & quot
+5.3k Golang : Generate Interleaved 2 inch by 5 inch barcode
+23.7k Find and replace a character in a string in Go
+7.8k Golang : Reverse a string with unicode
+10.3k Golang : Embed secret text string into binary(executable) file
+12.9k Python : Convert IPv6 address to decimal and back to IPv6
+12.3k Golang : Display list of countries and ISO codes
+18k Golang : Get all upper case or lower case characters from string example
+9.4k Golang : Launch Mac OS X Preview (or other OS) application from your program example
+22.1k Golang : Repeat a character by multiple of x factor
+6.1k Golang : Measure execution time for a function
+6.5k Golang : Handling image beyond OpenCV video capture boundary