Created
December 24, 2019 10:51
-
-
Save heiwa4126/b3803d2f5f0cb14a7ffb82e489b4db78 to your computer and use it in GitHub Desktop.
Golangのbufio.Peek()で、EOFのときにエラーにしたくないときの処理。`err == bufio.ErrBufferFull`ではないところがコツ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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