Skip to content

Instantly share code, notes, and snippets.

@heiwa4126
Created December 24, 2019 10:51
Show Gist options
  • Save heiwa4126/b3803d2f5f0cb14a7ffb82e489b4db78 to your computer and use it in GitHub Desktop.
Save heiwa4126/b3803d2f5f0cb14a7ffb82e489b4db78 to your computer and use it in GitHub Desktop.
Golangのbufio.Peek()で、EOFのときにエラーにしたくないときの処理。`err == bufio.ErrBufferFull`ではないところがコツ
package main
import (
"bufio"
"fmt"
"io"
"log"
"strings"
)
func bufio1(r *bufio.Reader, n int) {
data, err := r.Peek(n)
if err == nil || err == io.EOF {
fmt.Println(string(data))
return
}
log.Fatal(err)
}
func main() {
r := bufio.NewReader(strings.NewReader("世界の皆さん、こんにちは!"))
bufio1(r, 6)
bufio1(r, 12)
bufio1(r, 24)
bufio1(r, 1024)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment