Check data from channel

Report a typo

Nikola Tesla knows a lot about conductors. And he thought the channels in Go were similar. The receiver must receive no more than 7 data items from the channel. This limit is set by the Attempts constant. But it should stop when the channel is closed.

But something went wrong and now it doesn't always work. Now the receiver receives 7 data items every time.

Help Nicola. Correct the receiver so that it outputs only the data sent to the channel before channel closing.

Sample Input 1:

3

Sample Output 1:

0
1
2

Sample Input 2:

10

Sample Output 2:

0
1
2
3
4
5
6
Write a program in Go
package main

import "fmt"

const Attempts = 7

// set the condition in this function to stop the loop in time
func receiver(ch chan int) {
for range [Attempts]struct{}{} {
fmt.Println(<-ch)
}
}

// do not change the code bellow
func sender(send int) chan int {
ch := make(chan int, send)

go func() {
defer close(ch)
for i := 0; i < send; i++ {
ch <- i
}
}()
return ch
}

func main() {
var send int
fmt.Scan(&send)

ch := sender(send)
receiver(ch)
}
___

Create a free account to access the full topic