我如何在Go中高效方便地解析一个简单的消息?

huangapple go评论99阅读模式
英文:

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

问题

这是一个消息的示例:

用户:tbone
位置:/whatever
时间:23:23:23

这是一条小消息。

它有点类似于HTTP和其他协议:首部后面是一个空行和消息体。

解析这个消息的最简单高效的方法是什么?标准的text/scanner对我来说看起来不错。如果我能更好地处理空白字符就更好了。在首部中,它应该忽略冒号周围的空白字符,但要让我知道单词之间和换行之间的空格。我还需要知道消息体何时开始。

扫描器是否是完成这个任务的正确工具?是否有更好的策略?我是否应该编写自己的小解析器,逐个字符(或有时两个字符)地构建我的数据结构?那是我想要避免的不便,但我可以这样做。

顺便说一下,我控制消息格式。是否有更好的消息格式可以简化任务?

英文:

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?

答案1

得分: 5

text/scanner对于这么简单的格式来说,开销太大(在程序员时间上)。

net/http使用net/textproto,你可能也可以使用它。查找MIMEHeader和ReadMIMEHeader

如果你需要比MIMEHeader更复杂的内容,我会考虑使用JSON。

英文:

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.

huangapple
  • 本文由 发表于 2013年3月27日 14:07:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/15652823.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定