Using an indeterminate channel type

One trick that can often come in handy, and we haven't yet addressed, is the ability to have what is effectively a typeless channel.

If you're wondering why that might be useful, the short answer is concise code and application design thrift. Often this is a discouraged tactic, but you may find it useful from time to time, especially when you need to communicate one or more disparate concepts across a single channel. The following is an example of an indeterminate channel type:

package main import ( "fmt" "time" ) func main() { acceptingChannel := make(chan interface{}) go func() { acceptingChannel <- "A text message" time.Sleep(3 * time.Second) acceptingChannel <- false }() for { select { case msg := <- acceptingChannel: ...

Get Go: Building Web Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.