加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

golang的bufio用于内容解析

发布时间:2020-12-16 18:35:26 所属栏目:大数据 来源:网络整理
导读:golang提供了io.Reader,也就是读内容,可以从很多地方读,譬如: // from string. var r io.Reader = strings.NewReader( string ( "hello,world" )) // from bytes. var r io.Reader = bytes.NewReader([] byte ( "hello,world!" )) // from bytes buffer.

golang提供了io.Reader,也就是读内容,可以从很多地方读,譬如:

// from string.
var r io.Reader = strings.NewReader(string("hello,world"))
// from bytes.
var r io.Reader = bytes.NewReader([]byte("hello,world!"))
// from bytes buffer.
var r io.Reader = bytes.NewBuffer([]byte("hello,world"))

这个看起来没有啥稀奇的,就是从字符串或者字节读取呗。最后一个bytes.Buffer是写(Write)时增加到末尾,读(Read)时从头开始读的一个对象,就是个缓冲区。

还有一个比较有用的,带缓冲区的io:

// buffer reader
var r io.Reader = bufio.NewReader(strings.NewReader(string("hello,world")))

这个是和带缓冲区的写入是对应的。这个类是给网络reader用的,譬如协议解析,需要看看下面几个字节是什么,然后再解析之类的(Peek)。或者写入时,不断将小的bytes写入,最后Flush之类。

另外,bufio.Reader还提供了特殊的读法,譬如读取字符串(因为有缓冲区所以能读得出来)。

// ReadString reads until the first occurrence of delim in the input,// returning a string containing the data up to and including the delimiter.
// If ReadString encounters an error before finding a delimiter,// it returns the data read before the error and the error itself (often io.EOF).
// ReadString returns err != nil if and only if the returned data does not end in
// delim.
// For simple uses,a Scanner may be more convenient.
func (b *Reader) ReadString(delim byte) (line string,err error) {

对于内容解析,譬如解析下面的json,支持///**/风格的注释,那么就可以用scanner,也就是特定格式的扫描提取:

s := bufio.NewScanner(strings.NewReader("/*block comments*///line commentsn{}"))
s.Split(func(data []byte,atEOF bool) (advance int,token []byte,err error){
    // read more.
    return 0,nil,nil
})
for s.Scan() {
    fmt.Println(s.Text())
}
fmt.Println("err is",s.Err())

只需要判断是否需要的数据是否有满足,譬如//n是否成对,如果不成对就再读取。这样就大大降低了复杂逻辑了。

具体实现,参考:go-oryx

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读