Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

golang
channels
communitycreator

How to create and use buffered channels in Golang

Jeyabalaji Subramanian

Introduction

We can specify a fixed capacity for a channel and create a buffered channel.

If the channel has a buffer, the sender will only block until the value has been pushed into the buffer. If the buffer is full, the sender blocks until a receiver retrieves a value.

Let us look at a worked-out example to further understand this concept.

package main

import "fmt"

func main() {
	fmt.Printf("Hello World \n")
	ch := make(chan string, 2)

	ch <- "hello "
	ch <- "golang"

	fmt.Printf(<-ch)
	fmt.Printf(<-ch)
}

In the above example, we see that we can send two values into channel ch without blocking since the capacity is 2.

Deadlock scenario

However, if we try to overfill the buffer, we will receive a deadlock error. This is shown in the example below.

package main

import "fmt"

func main() {
	ch := make(chan int, 4)
	ch <- 1
	ch <- 2
	ch <- 3
	ch <- 4
	ch <- 5
	fmt.Println(<-ch)
	fmt.Println(<-ch)
	fmt.Println(<-ch)
	fmt.Println(<-ch)
}

Uses of buffered channels

One of the main uses of buffered channels is to limit the number of workers queued up. This is particularly useful in network I/O or Disk I/O intensive compute scenarios.

RELATED TAGS

golang
channels
communitycreator

CONTRIBUTOR

Jeyabalaji Subramanian
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring