I have a "key1:value1; key2:value2" like string (string with key:value
pattern concated by ;
).
Now I wish to parse this string to a Go struct:
type CustomStruct struct {
KeyName1 string `name:"key1" somevalidation:"xxx"`
KeyName2 int `name:"key2" somevalidation:"yyy"`
}
In the above example, the struct tag defines the name of the key in the string and can provide some validation for its corresponding value (it can set a default value if validation fails). For instance, KeyName2
is an int
value, so I wish the somevalidation
can check whether the KeyName2
satisfy, let's say, greater than 30 and less equal than 100.
And in another senario, I can define another struct CustomStruct2
for string like key3:value3; key4:value4;
How can I archive this kind of requirement efficiently and elegantly?