4

Here's an example of a message:

User: tbone
Location: /whatever
Time: 23:23:23

This is a little message.

It's sort of HTTP- and other protocols-ish: headers followed by a blank line and the message body.

What's the easiest pretty efficient 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 whitespace a little better. Namely, in the headers, it should ignore whitespace surrounding a colon, but let me know about spaces between words and newlines. I also need to know when the message body starts.

Might a scanner be the right tool for the job? Is there a better strategy? Should I just write my own little parser that marches along a character (or sometimes two) at a time and builds up my data structure? That's the kind of inconvenience I'd like to avoid, but I could do that.

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

4

1 に答える 1

5

text/scanner would be way too much overhead (in programmer time) to use for a format this simple.

net/http uses net/textproto, you might be able to use that too. Look for MIMEHeader and ReadMIMEHeader.

If you need something more complicated than what MIMEHeader can contain, I'd consider just using JSON.

于 2013-03-27T06:13:20.280 に答える