SlideShare a Scribd company logo
Channels Use Cases
Erhan Yakut / @yakuter
GopherCon Turkey / 18-19 December 2021
Programlama Dilleri
Aktif olarak Go ile geliştirme yapmakla
birlikte uzun yıllar PHP backend
developer olarak proje geliştirdim.
İletişim Bilgisi
Eposta : yakuter@gmail.com
Twitter: @yakuter
İş / Görev
Binalyze isimli Enterprise Forensics
yazılım şirketinde Senior Software
Architect olarak çalışmaktayım.
Tecrübe/Bilgi
Yaklaşık 15+ yıldır yazılım geliştirme ile
ilgilenmekte olup, şu anda işletim
sistemleri üzerinde olay sonrası delillerin
toplanması için yazılım geliştirmekteyim.
Erhan YAKUT
(yakuter)
Biyografi
Ben Kimim?
Why do we need channels?
Goroutines need to communicate each other to synchronize or exchange data with
each other. The solution is channels.
What is channel?
Channels are the pipes that connect concurrent
goroutines. You can send values into channels from one
goroutine and receive those values into another
goroutine.
Channels are a typed conduit through which you can
send and receive values with the channel operator, <-.
ch <- v // Send v to channel ch.
v := <-ch // Receive from ch, and assign value to v.
(The data flows in the direction of the arrow.)
Like maps and slices, channels must be created before
use:
ch := make(chan int)
Closing channels
Senders have the ability to close the channel to notify
receivers that no more data will be sent on the channel.
Receivers can use an additional variable while receiving
data from the channel to check whether the channel has
been closed.
v, ok := <- ch
If ok is false it means that we are reading from a closed
channel.
The value read from a closed channel will be the zero
value of the channel's type. For example, if the channel is
an int channel, then the value received from a closed
channel will be 0.
Only the sender should close a channel, never the
receiver. Sending on a closed channel will cause a panic.
Buffered channels and range
It is possible to create a channel with a buffer. Sends to a
buffered channel are blocked only when the buffer is full.
Similarly receives from a buffered channel are blocked
only when the buffer is empty.
Buffered channels can be created by passing an
additional capacity parameter to the make function
which specifies the size of the buffer.
ch := make(chan type, capacity)
The loop for v := range ch receives values from the
channel repeatedly until it is closed.
Worker pool
In computer programming, a worker pool is a software
design pattern for achieving concurrency of execution in
a computer program.
A worker pool maintains multiple threads waiting for
tasks to be allocated for concurrent execution by the
supervising program.
Select usage
The select statement is used to choose from multiple
send/receive channel operations. The select statement
blocks until one of the send/receive operations is ready. If
multiple operations are ready, one of them is chosen at
random.
The syntax is similar to switch except that each of the
case statements will be a channel operation.
Check context with select and ticker
Tickers are for when you want to do something
repeatedly at regular intervals.
Tickers use a similar mechanism to timers: a channel that
is sent values.
In the example, we’ll use the select builtin on the channel
to await the values as they arrive every interval.
Channels for notification
1-to-1 notification
Notifications can be viewed as special requests/responses
in which the responded values are not important.
Generally, we use the blank struct type struct{} as the
element types of the notification channels, for the size of
type struct{} is zero, hence values of struct{} doesn't
consume memory.
Start/stop signal
If we queue up lots of goroutines, we can have them all
start at the same time by closing the signal channel.
Start/stop signal with waitgroup
To wait for multiple goroutines to finish, we can use a
wait group.
This WaitGroup is used to wait for all the goroutines
launched here to finish. Note: if a WaitGroup is explicitly
passed into functions, it should be done by pointer.
Launch several goroutines and increment the WaitGroup
counter for each.
Avoid re-use of the same i value in each goroutine
closure.
https://go.dev/doc/faq#closures_and_goroutines
Scheduled notification
We can use a timer or sleep function to wait for desired
time and then send signal (notification)
Please note, <-time.After(aDuration) will make the
current goroutine enter blocking state, but a
time.Sleep(aDuration) function call will not.
The use of <-time.After(aDuration) is often used in the
timeout mechanism which will be introduced below.
Use channels as mutex locks
Channels are blocking actions. So we can use them as
mutex locks.
There are two manners to use one-capacity buffered
channels as mutex locks.
- Lock through a send, unlock through a receive.
- Lock through a receive, unlock through a send.
The following is a lock-through-send example.
Use channels as multi access mutexes
Buffered channels can be used as multi access mutexes.
If the capacity of a channel is N, then it can be viewed as
a lock which can have most N owners at any time.
Counting semaphores are often used to enforce a
maximum number of concurrent requests.
Is channel closed
An nice useful isClosed function
Resources
https://go.dev/tour/concurrency/2
https://golangbot.com/channels/
https://go101.org/article/channel-use-cases.html
https://medium.com/@trevor4e/learning-gos-concurrency-through-illustrations-8c4aff603b3
Thank You

More Related Content

PPTX
Developing Cross Platform Applications with Golang
PDF
Building Command Line Tools with Golang
PDF
[INNOVATUBE] Tech Talk #3: Golang - Takaaki Mizuno
PPTX
Wonders of Golang
PDF
Getting started with Go - Florin Patan - Codemotion Rome 2017
PPT
Where's the source, Luke? : How to find and debug the code behind Plone
PDF
[INNOVATUBE] Tech Talk #3: Golang - Vũ Nguyễn
PDF
Go language presentation
Developing Cross Platform Applications with Golang
Building Command Line Tools with Golang
[INNOVATUBE] Tech Talk #3: Golang - Takaaki Mizuno
Wonders of Golang
Getting started with Go - Florin Patan - Codemotion Rome 2017
Where's the source, Luke? : How to find and debug the code behind Plone
[INNOVATUBE] Tech Talk #3: Golang - Vũ Nguyễn
Go language presentation

What's hot (20)

PDF
Lennart Regebro What Zope Did Wrong (And What To Do Instead)
PPTX
Golang - Overview of Go (golang) Language
PDF
Why you should care about Go (Golang)
PPT
Introduction to Go-Lang
PDF
Internationalizing Ubuntu apps
PDF
Lets Go - An introduction to Google's Go Programming Language
PPTX
What’s new in CSharp6
PPTX
Python Programming Essentials - M2 - Introduction to Python
PPTX
Introduction to scripts
PPTX
scriptcs - scripted C#, REPL and script extensibility
PPTX
PDF
Windows script host
PDF
Windows batch scripting
PPT
A First Look at Google's Go Programming Language
ODP
Glance rebol
PDF
Feedback from an eclipse plugin developer to provide support to large set of ...
PPTX
Python Programming Essentials - M3 - Python Installation
PDF
Create a PHP Library the right way
PPTX
'BUILDING ANGULAR APPS WITH NX' by Anastasia Necheporenko
PPTX
Introduction to go lang
Lennart Regebro What Zope Did Wrong (And What To Do Instead)
Golang - Overview of Go (golang) Language
Why you should care about Go (Golang)
Introduction to Go-Lang
Internationalizing Ubuntu apps
Lets Go - An introduction to Google's Go Programming Language
What’s new in CSharp6
Python Programming Essentials - M2 - Introduction to Python
Introduction to scripts
scriptcs - scripted C#, REPL and script extensibility
Windows script host
Windows batch scripting
A First Look at Google's Go Programming Language
Glance rebol
Feedback from an eclipse plugin developer to provide support to large set of ...
Python Programming Essentials - M3 - Python Installation
Create a PHP Library the right way
'BUILDING ANGULAR APPS WITH NX' by Anastasia Necheporenko
Introduction to go lang
Ad

Similar to Golang Channels use cases (20)

PDF
Golang Channels
PDF
A Channel Compendium
PDF
Go concurrency
PDF
Go courseday3
PDF
Goroutines and Channels in practice
PDF
Go Concurrency
PDF
An Introduction to Go
PPTX
Go Concurrency Basics
PDF
Concurrency in Go by Denys Goldiner.pdf
ODP
Go! here we go!
PDF
Go Concurrency
PPTX
Fundamental concurrent programming
PDF
Concurrency in Golang
KEY
Google Go Overview
PDF
The async/await concurrency pattern in Golang
PDF
Learn channel-from-from-golang-source-code
PDF
LCA2014 - Introduction to Go
PDF
Concurrency patterns
PDF
Channel in Go
PDF
Concurrency with Go
Golang Channels
A Channel Compendium
Go concurrency
Go courseday3
Goroutines and Channels in practice
Go Concurrency
An Introduction to Go
Go Concurrency Basics
Concurrency in Go by Denys Goldiner.pdf
Go! here we go!
Go Concurrency
Fundamental concurrent programming
Concurrency in Golang
Google Go Overview
The async/await concurrency pattern in Golang
Learn channel-from-from-golang-source-code
LCA2014 - Introduction to Go
Concurrency patterns
Channel in Go
Concurrency with Go
Ad

Recently uploaded (20)

PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Nekopoi APK 2025 free lastest update
PPTX
Transform Your Business with a Software ERP System
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PPTX
Essential Infomation Tech presentation.pptx
PDF
System and Network Administraation Chapter 3
PDF
AI in Product Development-omnex systems
PDF
top salesforce developer skills in 2025.pdf
PDF
medical staffing services at VALiNTRY
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Digital Strategies for Manufacturing Companies
PPTX
Operating system designcfffgfgggggggvggggggggg
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PPTX
Introduction to Artificial Intelligence
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PPTX
Reimagine Home Health with the Power of Agentic AI​
Which alternative to Crystal Reports is best for small or large businesses.pdf
Nekopoi APK 2025 free lastest update
Transform Your Business with a Software ERP System
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Essential Infomation Tech presentation.pptx
System and Network Administraation Chapter 3
AI in Product Development-omnex systems
top salesforce developer skills in 2025.pdf
medical staffing services at VALiNTRY
Design an Analysis of Algorithms I-SECS-1021-03
Digital Strategies for Manufacturing Companies
Operating system designcfffgfgggggggvggggggggg
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Introduction to Artificial Intelligence
Internet Downloader Manager (IDM) Crack 6.42 Build 41
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Reimagine Home Health with the Power of Agentic AI​

Golang Channels use cases

  • 1. Channels Use Cases Erhan Yakut / @yakuter GopherCon Turkey / 18-19 December 2021
  • 2. Programlama Dilleri Aktif olarak Go ile geliştirme yapmakla birlikte uzun yıllar PHP backend developer olarak proje geliştirdim. İletişim Bilgisi Eposta : yakuter@gmail.com Twitter: @yakuter İş / Görev Binalyze isimli Enterprise Forensics yazılım şirketinde Senior Software Architect olarak çalışmaktayım. Tecrübe/Bilgi Yaklaşık 15+ yıldır yazılım geliştirme ile ilgilenmekte olup, şu anda işletim sistemleri üzerinde olay sonrası delillerin toplanması için yazılım geliştirmekteyim. Erhan YAKUT (yakuter) Biyografi Ben Kimim?
  • 3. Why do we need channels? Goroutines need to communicate each other to synchronize or exchange data with each other. The solution is channels.
  • 4. What is channel? Channels are the pipes that connect concurrent goroutines. You can send values into channels from one goroutine and receive those values into another goroutine. Channels are a typed conduit through which you can send and receive values with the channel operator, <-. ch <- v // Send v to channel ch. v := <-ch // Receive from ch, and assign value to v. (The data flows in the direction of the arrow.) Like maps and slices, channels must be created before use: ch := make(chan int)
  • 5. Closing channels Senders have the ability to close the channel to notify receivers that no more data will be sent on the channel. Receivers can use an additional variable while receiving data from the channel to check whether the channel has been closed. v, ok := <- ch If ok is false it means that we are reading from a closed channel. The value read from a closed channel will be the zero value of the channel's type. For example, if the channel is an int channel, then the value received from a closed channel will be 0. Only the sender should close a channel, never the receiver. Sending on a closed channel will cause a panic.
  • 6. Buffered channels and range It is possible to create a channel with a buffer. Sends to a buffered channel are blocked only when the buffer is full. Similarly receives from a buffered channel are blocked only when the buffer is empty. Buffered channels can be created by passing an additional capacity parameter to the make function which specifies the size of the buffer. ch := make(chan type, capacity) The loop for v := range ch receives values from the channel repeatedly until it is closed.
  • 7. Worker pool In computer programming, a worker pool is a software design pattern for achieving concurrency of execution in a computer program. A worker pool maintains multiple threads waiting for tasks to be allocated for concurrent execution by the supervising program.
  • 8. Select usage The select statement is used to choose from multiple send/receive channel operations. The select statement blocks until one of the send/receive operations is ready. If multiple operations are ready, one of them is chosen at random. The syntax is similar to switch except that each of the case statements will be a channel operation.
  • 9. Check context with select and ticker Tickers are for when you want to do something repeatedly at regular intervals. Tickers use a similar mechanism to timers: a channel that is sent values. In the example, we’ll use the select builtin on the channel to await the values as they arrive every interval.
  • 10. Channels for notification 1-to-1 notification Notifications can be viewed as special requests/responses in which the responded values are not important. Generally, we use the blank struct type struct{} as the element types of the notification channels, for the size of type struct{} is zero, hence values of struct{} doesn't consume memory.
  • 11. Start/stop signal If we queue up lots of goroutines, we can have them all start at the same time by closing the signal channel.
  • 12. Start/stop signal with waitgroup To wait for multiple goroutines to finish, we can use a wait group. This WaitGroup is used to wait for all the goroutines launched here to finish. Note: if a WaitGroup is explicitly passed into functions, it should be done by pointer. Launch several goroutines and increment the WaitGroup counter for each. Avoid re-use of the same i value in each goroutine closure. https://go.dev/doc/faq#closures_and_goroutines
  • 13. Scheduled notification We can use a timer or sleep function to wait for desired time and then send signal (notification) Please note, <-time.After(aDuration) will make the current goroutine enter blocking state, but a time.Sleep(aDuration) function call will not. The use of <-time.After(aDuration) is often used in the timeout mechanism which will be introduced below.
  • 14. Use channels as mutex locks Channels are blocking actions. So we can use them as mutex locks. There are two manners to use one-capacity buffered channels as mutex locks. - Lock through a send, unlock through a receive. - Lock through a receive, unlock through a send. The following is a lock-through-send example.
  • 15. Use channels as multi access mutexes Buffered channels can be used as multi access mutexes. If the capacity of a channel is N, then it can be viewed as a lock which can have most N owners at any time. Counting semaphores are often used to enforce a maximum number of concurrent requests.
  • 16. Is channel closed An nice useful isClosed function