package main

import (
    "strings"
    "bufio"
    "fmt"
    "os"
)

func main() {
    /////////////////读
    strReader := strings.NewReader("hello world") //直接读取字符串
    bufReader := bufio.NewReader(strReader)
    data, _ := bufReader.Peek(5) //只读不取
    fmt.Println(string(data), bufReader.Buffered()) //bufReader.Buffered()是缓存的字符数,此处正好是将hello world都缓存起来了
    str, _ := bufReader.ReadString(' ') //取出来,所以下面的bufReader.Buffered()成了5
    fmt.Println(str, bufReader.Buffered())

    /////////////////写
    w := bufio.NewWriter(os.Stdout)
    //将Hello和world写入到w,但还要w.Flush()才能真正写入
    fmt.Fprint(w, "Hello")
    fmt.Fprint(w, "world")
    w.Flush()
}

输出:
bufio读写_第1张图片