Categories
bufio Standard Library

Reader

type Reader struct {
	buf          []byte
	rd           io.Reader // reader provided by the client
	r, w         int       // buf read and write positions
	err          error
	lastByte     int // last byte read for UnreadByte; -1 means invalid
	lastRuneSize int // size of last rune read for UnreadRune; -1 means invalid
}
// NewReader returns a new Reader whose buffer has the default size.
func NewReader(rd io.Reader) *Reader {
	return NewReaderSize(rd, defaultBufSize)
}
package main

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

func main() {
	scanner := bufio.NewReader(os.Stdin)
	fmt.Print("Enter text: ")
	text,_ := scanner.ReadString('\n')
	fmt.Printf("You entered: %s", text)
}
Categories
bufio Standard Library

Writer and Flush

package main

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

func main() {
	w := bufio.NewWriter(os.Stdout)
	fmt.Fprint(w, "Hello, ")
	fmt.Fprint(w, "world!")
	w.Flush() // Don't forget to flush!
}
Categories
bufio Standard Library

What is a buffer?

Send data to a buffer (system memory) and will not write until it reaches the length. After which it writes the data and flushes the buffer.