【Go 语言社区】golang的bufio用于内容解析

时间:2022-05-04
本文章向大家介绍【Go 语言社区】golang的bufio用于内容解析,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

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 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是否成对,如果不成对就再读取。这样就大大降低了复杂逻辑了。