All-in-one select

Report a typo

When using the select statement, you can combine read-write operations.

The code below declares two channels. Your task is to write the additional required code with three actions in the select statement:

  • Reading data from the inputChan channel.

  • Writing data to the outputChan channel.

  • Exiting the program if read-write operations are not possible.

You can write any number to outputChan. The data read from inputChan must be printed. Finally, print the message "exit" at the end of the program.

This problem uses the hidden check() function to check your solution for this coding task.
Write a program in Go
package main

import "fmt"

var inputChan = make(chan int, 1) // nolint:gochecknoglobals
var outputChan = make(chan int, 1) // nolint:gochecknoglobals

func main() {
check() // DO NOT delete this line! it is used to check your code

// Write the missing code below!
for {
select {
case input := ?:
fmt.Println(?)
case outputChan ?:
default:
fmt.Println("?")
return
}
}
}

Create a free account to access the full topic