Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

found errors in chapter 16, activity 16.02 #4

Open
PetyaKatsarova opened this issue Jan 16, 2024 · 0 comments
Open

found errors in chapter 16, activity 16.02 #4

PetyaKatsarova opened this issue Jan 16, 2024 · 0 comments

Comments

@PetyaKatsarova
Copy link

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
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant