Golang : does not implement flag.Value (missing Set method)
Keep getting this error message : does not implement flag.Value (missing Set method) while trying to write example for flag.Var() function today. Apparently, to assign value with flag.Var() function to a variable in a struct. You need to implement Set() and Get() method. It is helpful to have String() method as well.
To fix this error, all you need to do is the add the Set method.
For example :
package main
import (
"flag"
"fmt"
)
type flagStr struct {
value *string
}
// compiler will throw out missing Set method
// if this method below is ... well.. missing
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)
}
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+9.3k Golang : Launch Mac OS X Preview (or other OS) application from your program example
+6.7k Golang : Calculate pivot points for a cross
+15.2k Golang : Get all local users and print out their home directory, description and group id
+25.6k Golang : missing Mercurial command
+16.8k Golang : read gzipped http response
+6.5k Golang : How to determine if request or crawl is from Google robots
+13.5k Golang : Query string with space symbol %20 in between
+12.4k Elastic Search : Return all records (higher than default 10)
+30.5k Golang : Remove characters from string example
+10.5k Golang : ISO8601 Duration Parser example
+8.7k Golang : Take screen shot of browser with JQuery example
+10.1k Golang : How to get quoted string into another string?