You should really use two channels. Trying to make it work with one channel will be messy.
There's a Google Sites on the Producer/Consumer pattern that may be useful.
For the producer to know what the consumer is responding to, you could use a struct for the response:
type responseMessage struct {
Request string
Response string
}
var requests chan string
var responses chan *responseMessage
func processRequests() {
for {
request <- requests
// Process request...
responses <- &responseMessage{request, "some response string"}
}
}
func processResponses() {
someResponseMessage := <- responses
if someResponseMessage.Request == "doSomething" {
// do something!
}
}
func main() {
requests = make(chan string)
responses = make(chan *responseMessage)
go processRequests()
go processResponses()
requests <- "doSomething"
requests <- "doSomethingElse"
requests <- "etc"
select {} // Block forever
}