Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
package main
import (
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", "")
fmt.Println("version = ", version)
fmt.Println("profile = ", profile)
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