// Unformatted
cluid=e2PADQ8fVw9MhhxdN3fjlUSSZTQ= cldbid=3 name=Toby\son\slinux
// Formatted
{
{"cluid": "e2PADQ8fVw9MhhxdN3fjlUSSZTQ="},
{"cldbid": "3"},
{"name": "Toby\son\slinux"},
}
Here is some code I use to do something similar to this.
//Response contains the formatting for telnet responses in a key:pair format
type Response struct {
Key string
Value string
}
//FmtResponse takes a string response from the ServerQuery and returns a formatted string array
func FmtResponse(input string) ([]Response, error) {
if input != "" {
var ResponseList []Response
fields := strings.Fields(input)
for _, e := range fields {
keyvalue := strings.SplitN(e, "=", 2)
new := Response{
Key: Sanitize(keyvalue[0]),
Value: Sanitize(keyvalue[1]),
}
newList := append(ResponseList, new)
ResponseList = newList
}
//Broke out of for loop and returns list
return ResponseList, nil
}
return nil, errors.New("input is empty")
}
// Unparsed
Toby\son\slinux
//Parsed
Toby on linux
//Sanitize trims spaces and replaces escape codes with their readable counterparts.
func Sanitize(input string) string {
res := strings.TrimSpace(input)
res = strings.Replace(res, `\s`, " ", -1)
return res
}
func listener(notif chan Notificaion) {
for _, e := range notif {
fmt.Printf("Notification: %s", e)
}
}
These are just my ideas, and I'd love some feedback on them.