golang的bufio用于内容解析

golang的bufio用于内容解析,第1张

概述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 byt

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 readervar 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 comments\n{}"))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

总结

以上是内存溢出为你收集整理的golang的bufio用于内容解析全部内容,希望文章能够帮你解决golang的bufio用于内容解析所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/langs/1286006.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-09
下一篇 2022-06-09

发表评论

登录后才能评论

评论列表(0条)

保存