Replace Scanner with Reader
allows to test code on Go version < 1.6. where
`scanner.Buffer` is not present
@@ -3,6 +3,7 @@ package main
import (
"bufio"
"fmt"
+ "io"
"net/http"
)
@@ -18,16 +19,21 @@ func main() {
fmt.Println("Response status:", resp.Status)
- scanner := bufio.NewScanner(resp.Body)
- buf := make([]byte, 0, bufio.MaxScanTokenSize)
- scanner.Buffer(buf, maxScanTokenSize)
+ reader := bufio.NewReaderSize(resp.Body, maxScanTokenSize)
- for i := 0; scanner.Scan() && i < 5; i++ {
- fmt.Println(scanner.Text())
+ for {
+ token, _, err := reader.ReadLine()
+ if len(token) > 0 {
+ fmt.Println(string(token))
}
- if err := scanner.Err(); err != nil {
+ if err != nil {
+ if err == io.EOF {
+ break
+ } else {
panic(err)
}
+ }
+ }
// force close the connection to produce a net.close event
// without this, we would have to wait for a timeout;
