How to do, convert string to struct?

Hi!
How to do it?
Convert strings strN to map structures:
type Triggers struct {
Triggerid int map:"triggerid"
Lastchange int map:"lastchange"
Priority int map:"priority"
Description string map:"description"
Status int map:"status"
URL string map:"url"
Value string map:"value"
DepLevel int map:"dep_level"
Comments string map:"commets"
Error string map:"error"
TemplateID string map:"templateid"
Expression string map:"expression"
Type string map:"type"
}
str1 := "[map[description:{HOSTNAME} Warning! url: status:0 error: templateid:13490 expression:{17081}>90 value:0 priority:4 lastchange:1500450485 dep_level:0 comments: type:0 triggerid:15757]]"
str2 := "[map[dep_level:0 comments: error: type:0 triggerid:15756 expression:{17080}>35 description:{HOSTNAME} Vysokaya temperatura vypriamitelya 3 lastchange:1500450495 templateid:13508 url: status:0 value:0 priority:4]]"
str3 := “[map[description:{HOSTNAME} Vysokaya temperatura vypriamitelya 1 lastchange:1500450504 error: templateid:13496 value:0 priority:4 dep_level:0 comments: triggerid:15754 expression:{17078}>35 url: status:0 type:0]]”

Regards!

Can you please give a more detailed question about what you have, you get, and want to have?

And also please do us a favor and edit your code to contain either a properly fenced code block or an properly indented one. It is very hard to even distinguish your code snippet from the rest of the post.

We have:

str1 := “[map[description:{HOSTNAME} Warning! CPU url: status:0 error: templateid:13490 expression:{17081}>90 value:0 priority:4 lastchange:1500450485 dep_level:0 comments: type:0 triggerid:15757]]”

fmt.Printf(“Result: %T\t%+v\n”, str1, str1)

Result: string [map[value:0 priority:3 description:{HOSTNAME} Warning! CPU url: status:0 dep_level:0 comments: error: templateid:15523 type:0 triggerid:15524 expression:{16664}>95 lastchange:1500454055]]

want to get:

fmt.Printf(“Result: %T\t%+v\n”, str1.Priority, str1.Priority)
Result: int 3

fmt.Printf(“Result: %T\t%+v\n”, str1.Description, str1.Description)
Result: string Warning! CPU

Regards!

Again, please use proper markup to make code and prose distinguishable.

Anyway, it seems as if you had a complex input format which you need to parse.

Perhaps you can build something using the encoding package or its subpackages, but I rreally doubt it.

I do think you need a much more sophisticated parsing library and think of how to formulate a proper syntax for your language.


But where is your data from? Have you control over the datasource? can you make it spitting out proper JSON or some binary format? Those where much better to deserialize than what you have now…

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.