Created
June 12, 2021 01:20
-
-
Save oscarzhou/67adad43f18448ff0490c27ff644f468 to your computer and use it in GitHub Desktop.
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 ( | |
"encoding/json" | |
"flag" | |
"fmt" | |
) | |
type Profile struct { | |
Name string | |
Age uint | |
Verified bool | |
} | |
// new added | |
func (this *Profile) String() string { | |
b, _ := json.Marshal(*this) | |
return string(b) | |
} | |
// new added | |
func (this *Profile) Set(s string) error { | |
return json.Unmarshal([]byte(s), this) | |
} | |
func main() { | |
var version string | |
var profile Profile | |
flag.StringVar(&version, "setVersion", "1.0.0", "") | |
flag.Var(&profile, "setProfile", "") | |
flag.Parse() | |
fmt.Println("version = ", version) | |
fmt.Println("profile = ", profile) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If anyone is struggling with escaping the arguments, use:
go run . --setVersion 1 --setProfile \{\"name\":\"aa\",\"age\":22,\"verified\":true\}