Skip to content

found errors in chapter 16, activity 16.02 #4

@PetyaKatsarova

Description

@PetyaKatsarova

it is not reading the last numbers from the files leading to wrong sum of even and odd numbers due to returned on reaching EOF.
second error: it was reading '\r', so had to trim it too. I did a quick ugly solution:
func source(filename string, out chan int, wg *sync.WaitGroup) {
f, err := os.Open(filename)
if err != nil { panic(err) }

rd := bufio.NewReader(f)
for {
	str, err := rd.ReadString('\n')
	if err != nil {
		if err.Error() == "EOF" {
			i, err := strconv.Atoi(str) // ugly code- repetition but the job is done
			fmt.Println(i)
			if err != nil { panic(err) }
			out <- i
			f.Close()
			wg.Done()
			return
		} else { panic(err) }
	}
	str     = strings.TrimRight(str, "\r\n")
	i, err := strconv.Atoi(str)
	fmt.Println(i)
	if err != nil { panic(err) }
	out <- i
}

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions