Understanding blocking nature of Golang Channel

Code Setup

package mainimport (
"fmt"
"time"
)
func senderRepeat(c chan string) {
t := time.NewTicker(time.Second * 1)
for {
<-t.C
}
}
func main() {
stopChannel := make(chan string)
c := make(chan string)
go senderRepeat(c)
go func() {
time.Sleep(time.Second * 5)
fmt.Println("Running...")
stopChannel <- "stop"
fmt.Println("stop channel writes are non blocking")
}()
fmt.Println(<-c) }

Execution of program

$ go run example12_7.go
$ go run example12_7.go
Running...
fmt.Println(<-c)

--

--

--

Writing to soothe the soul, programming to achieve flow

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Explore Shimmer Animation Effect In Flutter

SQL Server data types

Let’s write our Storybook in Azure

TypeDB on Tour | April 2022

Firebase Token Validation via .NET Core API

SelectProjectTypeAPI

Rust for beginners — Part 9 — Struct

Principles of Great Code Review

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mipsmonsta

Mipsmonsta

Writing to soothe the soul, programming to achieve flow

More from Medium

How Golang DB migration tool work?

The Context Package In Go

How To Upgrade An Indirect Dependency In GoLang

Golang pprof singleton performance