parse json arguments from command-line flags

[Golang] How to parse the JSON arguments from the Command-line flags

Passing the JSON argument via the Command-line flag seems to be not typical, but we may need it in some cases. This post will show you how to implement it in Golang. The package flag provides an easy way to implement it. Let’s take a look at a simple example of using package flag first: package main import ( "flag" "fmt" ) func main() { var version string // parse the value of the flag setVersion to variable version flag.StringVar(&version, "setVersion", "1.0.0", "") flag.Parse() fmt.Println("version = ", version) } The output is ...

[Golang] reflect all

This post is about how to use reflect feature to implement as much as you want to do with Golang. Here are some cases: 1. How to get all the fields’ name of an object? 1type A struct{ 2 Foo string 3 Bar int64 4} 5 6func main() { 7 a := &A{Foo: "afoo"} 8 val := reflect.ValueOf(a).Elem() 9 for i:=0; i<val.NumField();i++{ 10 fmt.Println(val.Type().Field(i).Name) 11 } 12} The output: ...

DigitalOcean Referral Badge
Sign up to get $200, 60-day account credit !