0

I have a file with this content:

3
09:00 19:00
10
someText1
someText2
... 

First I want to read the first 3 lines of the configuration:

sourceFile, err := os.OpenFile(fileName, os.O_RDONLY, 0644)
cfg := ReadConfig(sourceFile)

func ReadConfig(r io.Reader) Config {
    scanner := bufio.NewScanner(r)
    var cfg Config
    for i := 0; i < 3 && scanner.Scan(); i++ {
        switch i {
        case 0:
            cfg.Parts, _ = strconv.Atoi(scanner.Text())
        case 1:
            timeParts := strings.Split(scanner.Text(), " ")
            cfg.Start, _ = time.Parse("15:04", timeParts[0])
            cfg.End, _ = time.Parse("15:04", timeParts[1])
        case 2:
            cfg.Val, _ = strconv.Atoi(scanner.Text())
        }
    }
    return cfg

}

They are read normally and now in another function I want to read the remaining ones:

Read(ctx, sourceFile)

func Read(ctx context.Context, r io.Reader) error {
    scanner := bufio.NewScanner(r)
    for scanner.Scan() {
            event := parseEvent(scanner.Text())
    }
    return nil
}

But reads do not occur because EOF is returned immediately. Why does this happen if I have read only 3 lines ?

Alpharius
  • 489
  • 5
  • 12

1 Answers1

1

bufio.Scanner uses a buffer internally (source code):

type Scanner struct {
    r            io.Reader
    split        SplitFunc
    maxTokenSize int
    token        []byte
    buf          []byte    // Buffer used as argument to split.
    //^^^^^^^^^^^^^^^^^
    start        int
    end          int
    err          error
    empties      int
    scanCalled   bool
    done         bool
}

The initial buffer size is 4096 (source code):

const (
    startBufSize = 4096 // Size of initial allocation for buffer.
)

And the first call to Scan reads up to startBufSize bytes from the underlying reader (source code).

You see that although you only reads 3 lines using the scanner, the scanner could read far more bytes into its buffer.

bufio.Reader has a similar behavior. See this question: Multiple Arrow CSV Readers on same file returns null.

Zeke Lu
  • 6,349
  • 1
  • 17
  • 23