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 ?