package main import ( "bufio" "fmt" "os" "strings" ) func main() { strReader := strings.NewReader("hello, world") bufReader := bufio.NewReader(strReader) //Peek: 提取不缓冲 data, _ := bufReader.Peek(5) fmt.Println(string(data), bufReader.Buffered()) //ReadString: 提取且缓冲 str, _ := bufReader.ReadString(' ') fmt.Println(str, bufReader.Buffered()) w := bufio.NewWriter(os.Stdout) fmt.Fprint(w, "Hello ") fmt.Fprint(w, "world!")
//buffer一定要记得flush w.Flush() }