How can I efficiently and conveniently parse a simple message in Go?

Here is an example post:

User: tbone Location: /whatever Time: 23:23:23 This is a little message. 

These are a kind of HTTP and other ish protocols: headers followed by an empty line and message body.

What is the easiest and most effective way to parse this? The standard text / scanner looks good to me. It would be very easy for me to use if I could deal with the gaps a little better. Namely, in the headers, he should ignore the spaces around the colon, but let me know about the spaces between words and newlines. I also need to know when the body of the message begins.

Can a scanner be the right tool for the job? Is there a better strategy? Should I just write my own little parser that marchs by character (or sometimes two) at a time and builds my data structure? I would like to avoid such inconveniences, but I could do it.

By the way, I control the message format. Is there a better message format to simplify the task?

+4
source share
1 answer

the text / scanner will be too expensive (in programmer time) to use this simple format.

net / http uses net / textproto , you can also use this. Find MIMEHeader and ReadMIMEHeader .

If you need something more complex than MIMEHeader can contain, I would only think about using JSON.

+5
source

All Articles