Buffered channel and its blocking behavior

Create a buffered channel

out := make(chan string, 5)

How about the blocking nature?

Demonstration of behavior

package bufferchannelsimport (
func GetContentHeaders(url string, out chan string, index int) {
client := &http.Client{}
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
resp, err := client.Do(req)
if err != nil {
defer resp.Body.Close()
fmt.Printf("%d. Before out<-\n", index)
out <- resp.Status
fmt.Printf("%d. After out<-\n", index) //don't see this means channel blocked


package mainimport (
func main() { out := make(chan string, 5) //1 url := "https://www.google.com"
for i := 0; i < 10; i++ {
go bufferchannels.GetContentHeaders(url, out, i)//2
for i := 0; i < 2; i++ { //3
fmt.Printf("response header: %q\n", <-out)
time.Sleep(time.Second * 20)//4
fmt.Println("main ended...")

So how may of standalone “Before out” statements will we see?

$ go run main.go
5. Before out<-
5. After out<-
response header: "200 OK"
0. Before out<-
0. After out<-
2. Before out<-
2. After out<-
9. Before out<-
9. After out<-
6. Before out<-
6. After out<-
1. Before out<-
1. After out<-
8. Before out<-
8. After out<-
7. Before out<-
response header: "200 OK"
3. Before out<-
4. Before out<-
main ended...
Photo by Hello I'm Nik on Unsplash




Writing to soothe the soul, programming to achieve flow

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

Recommended from Medium

Top Debugging Methods for Quicker Deployment in Java

Shopify Case Study — How Migration from Magento to Shopify Improved Store’s Revenue

5 Apps To Try This New Year

How to Use Simics Platform for Software Engineer Training

Leveling up your software delivery capabilities (part 2)

Experience of People at Renaissance!

The rhythm master

XOD powered line follower mBot

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


Writing to soothe the soul, programming to achieve flow

More from Medium

How Golang DB migration tool work?

Unit testing in Go using Mockery

The Context Package In Go

Golang Interfaces