GoLang Tutorial - Channels ("<-")
Continued from Goroutines, in this post, we'll learn how Goroutines communicate using channels.
chan T is a channel of type T:
These types are the types of data that the channels are allowed to transport. No other types are allowed to be transported using the channels.
Channels are a typed pipe through which we can send and receive values with the channel operator, "<-". So, the syntax to send and receive data from a channel are:
data := <- my_channel // read from channel my_channel my_channel <- data // write to channel my_channel
We used the time.Sleep() in Goroutines to make the main Goroutine wait for the hello Goroutine to finish:
Let's modify the code using channels:
The line of code marked in red-box (<-finished) is blocking. In other words, the control will not move to the next line of code until the Goroutine writes data to the finished channel. As we can guess, this eliminates the need for the time.Sleep() we used before to prevent the main Goroutine from exiting.
Note that the line <-finished receives data from the finished channel but does not use or store that data in any variable. This is perfectly legal.
Here is a modified version of sample code from Channels which sums the numbers in a slice, distributing the work between two Goroutines. Once both Goroutines have completed their computation, it calculates the final result.
By default, sends and receives block until the other side is ready. This allows Goroutines to synchronize without explicit locks or condition variables. In the code, we got the value x first, and it waits until y value arrives before printing out the sum x+y.
The following code shows several ways of calculating Fibonacci sequence, recursive and iterative (via closure or via channel):
The code: fibonacci-channel.go
Go Tutorial
- GoLang Tutorial - HelloWorld
- Calling code in an external package & go.mod / go.sum files
- Workspaces
- Workspaces II
- Visual Studio Code
- Data Types and Variables
- byte and rune
- Packages
- Functions
- Arrays and Slices
- A function taking and returning a slice
- Conditionals
- Loops
- Maps
- Range
- Pointers
- Closures and Anonymous Functions
- Structs and receiver methods
- Value or Pointer Receivers
- Interfaces
- Web Application Part 0 (Introduction)
- Web Application Part 1 (Basic)
- Web Application Part 2 (Using net/http)
- Web Application Part 3 (Adding "edit" capability)
- Web Application Part 4 (Handling non-existent pages and saving pages)
- Web Application Part 5 (Error handling and template caching)
- Web Application Part 6 (Validating the title with a regular expression)
- Web Application Part 7 (Function Literals and Closures)
- Building Docker image and deploying Go application to a Kubernetes cluster (minikube)
- Serverless Framework (Serverless Application Model-SAM)
- Serverless Web API with AWS Lambda
- Arrays vs Slices with an array left rotation sample
- Variadic Functions
- Goroutines
- Channels ("<-")
- Channels ("<-") with Select
- Channels ("<-") with worker pools
- Defer
- GoLang Panic and Recover
- String Formatting
- JSON
- SQLite
- Modules 0: Using External Go Modules from GitHub
- Modules 1 (Creating a new module)
- Modules 2 (Adding Dependencies)
- AWS SDK for Go (S3 listing)
- Linked List
- Binary Search Tree (BST) Part 1 (Tree/Node structs with insert and print functions)
- Go Application Authentication I (BasicAuth, Bearer-Token-Based Authentication)
- Go Application Authentication II (JWT Authentication)
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization