Go Channels: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 21: Line 21:


==Coordinate Multiple Pieces of Logic==
==Coordinate Multiple Pieces of Logic==
[[Go_Concurrency#Coordinate_Multiple_Pieces_of_Logic|Recommended pattern for channel use]].


=Timeout=
=Timeout=
=Cancellation=
=Cancellation=

Revision as of 00:09, 16 January 2024

External

Internal

TODO

Deplete Go_Channels into this.

Overview

The Go chans provide a composable, concurrent-safer way to communicate between concurrent processes.

Because channels are composable with other channels, this makes writing large systems simpler, because you can coordinate the input from multiple subsystems by easily composing the output together. You can combine input channels with timeouts, cancellations, or message to other subsystems.

The select statement is the complement to Go's channels. It is what enables all the difficult parts of composing channels. select statement allows you to wait for events, select a message from competing channels in a uniform random way, continue on if there are no messages waiting, and more.

Also see:

Go Concurrency Programming Models

Channel Patterns

Pipelines

Transferring the Ownership of Data

If you have a bit of code that produces a result and wants to share that result with another bit of code, what you are really doing is transferring the ownership of that data. Data has an owner, and one way to make concurrent programs safe is to ensure only one concurrent context has ownership of data at a time. Channels are the recommended way to implement this pattern in Go, as they help us communicate this concept by encoding the intent into the channel's type. You can create buffered channels to implement a cheap in-memory queue and thus decouple the producer from consumer. This pattern also makes you code composable with other concurrent code.

Coordinate Multiple Pieces of Logic

Recommended pattern for channel use.

Timeout

Cancellation