Skip to content

Instantly share code, notes, and snippets.

@oscarzhou
Created June 12, 2021 01:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oscarzhou/67adad43f18448ff0490c27ff644f468 to your computer and use it in GitHub Desktop.
Save oscarzhou/67adad43f18448ff0490c27ff644f468 to your computer and use it in GitHub Desktop.
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)
}
@dorgold
Copy link

dorgold commented May 23, 2022

If anyone is struggling with escaping the arguments, use:
go run . --setVersion 1 --setProfile \{\"name\":\"aa\",\"age\":22,\"verified\":true\}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment