• Reader and writer a low level, if you need to use them then go here to watch the lecture on these or look in the readers demo or exercises in the ztm directory
  • Here I’ll leave some reference for using the bufio package which makes reading and writing much easier
// bufio new reader
reader := strings.NewReader("SAMPLE")
buffered := bufio.NewReader(reader)
 
newString, err := buffered.ReadString('\n') // reads into newstring up to \n
if err == io.EOF {
	fmt.Println(newString)
} else {
	fmt.Println("Something went wrong when reading")
}
 
// bufio new scanner
scan := bufio.NewScanner(os.Stdin)
lines := make([]string, 0, 5)
for scan.Scan() {
	lines = append(lines, scan.Text())
}
if scanner.Err() != nil {
	// error with scanner
}
for _, line := range lines {
	fmt.Println(line)
}

Writer

 
buffer := strings.NewBufferString()
numBytes, err := buffer.WriteString("SAMPLE")
if err != nil {
	fmt.Println(err)
} else {
	fmt.Println(numBytes)
}

Reading from files