GOLANG PACKAGE
Understanding the context
package
Communication between goroutines can be troublesome at times using channels as the only communication medium. In this article, we are going to look at the context
package to send cancellation signals between API boundaries.
In a data and network intensive Go program, you probably going to use concurrency patterns where multiple goroutines, running concurrently or in parallel, are handling various tasks of the execution.
As we know, channels are the safest way to communicate between different goroutines. These channels carry data or messages between different goroutines. Channels can also be used to send cancellation signals across goroutines so that goroutines can stop their execution.
In the above program, we are creating a printNumbers
goroutine and then sleeping the main
goroutine for a second. Until then, the printNumbers
goroutine is running an infinite for
loop in the background.