How do I convert a string to a structure?
The line looks like this: Name[data1] Name2[data1 data2 data3] Name3[data1 data2] ...
The data can be of type Int, String, or Float. In general, this is not important, you can write everything as string.
Data in brackets is separated by a space
What I got using regexp:
func Parse() {
str := "Version[2f0] Terminal[10002] Machine[10.1.1.1] DH[0.137%] Temp[45] Fan[0] Vo[4074 4042 4058 4098] CRC[0 0 0 0]"
j := make(map[string][]string)
re, err := regexp.Compile(`(?P<Name>[^ \[]*)\[(?P<Value>[^\]]*)`)
if err != nil {
log.Fatal(err)
}
res := re.FindAllSubmatch([]byte(str), -1)
for _, match := range res {
j[string(match[1])] = strings.Split(string(match[2]), " ")
}
log.Printf("%+v", j)
}
Could there be a more elegant way using json.Unmarshal?