I created a function to transform a particular string (derived from [] bytes) into a struct. In the program I am building an encryption and a decryption are carried out and between these two passages the data are transferred via JSON. The function that I propose works, but at the time when the string becomes very long, processing times are very long. What I would like to ask is if anyone has any idea how to speed up this function or how to manage the situation better.
this is the function
func Datadecrypt(input []byte) Data {
s := string(input)
vals := strings.Replace(s," "," ",-1)
part := strings.Fields(vals)
c := strings.Split(s, "int=")[1]
co := strings.Split(c, ")")[0]
count, err := strconv.Atoi(co)
if err != nil {
log.Fatal(err)
}
var stringa string
for j := 0; j<len(part); j++ {
if strings.ContainsAny(part[j], ":") {
stringa = stringa+" "+part[j]
} else {
stringa = stringa+","+part[j]
}
}
var interpart []map[string]string
var result Data
var i = 0
for {
split := strings.Split(stringa, "[")[2+i]
splitend := strings.Split(split, "]")[0]
control := strings.Replace(splitend," "," ",-1)
mapst := mapstring.StrToMS(control)
interpart = append(interpart, mapst)
i++
if i >= count {
break
}
}
result = Data{Count: count, Results: interpart}
return result
}
this is the struct
type Data struct {
Count int `json:"count"`
Results []map[string]string `json:"result"`
}
this is the string that derives from s: = string (input)
"Count=\"%!s(int=107)\"
Results=\"[map[MATRICOLA:2 NOMEmy:value COGNOMEmy:value] map[MATRICOLA:7 NOMEmy:value COGNOMEmy:value] map[MATRICOLA:5 NOMEmy:value COGNOMEmy:value] map[MATRICOLA:6 NOMEmy:value COGNOMEmy:value]]\"
"
The string I wrote above is printed using JSON response
This is the input string NOT JSON
Count="%!s(int=107)"
Results="[map[MATRICOLA:2 NOMEmy:value COGNOMEmy:value] map[MATRICOLA:7 NOMEmy:value COGNOMEmy:value] map[MATRICOLA:5 NOMEmy:value COGNOMEmy:value] map[MATRICOLA:6 NOMEmy:value COGNOMEmy:value]]"
this is the result I get through the function
{107 map[MATRICOLA:2 NOMEmy:value COGNOMEmy:value] map[MATRICOLA:7 NOMEmy:value COGNOMEmy:value] map[MATRICOLA:5 NOMEmy:value COGNOMEmy:value] map[MATRICOLA:6 NOMEmy:value COGNOMEmy:value]]}
Thanks, in advance