Golang : Channels and buffered channels examples
Continue from previous tutorial on goroutines. Goroutines run in the same memory address space and how does Golang allow communications between different goroutines?
Golang has channels. Imagine channels as the pipes that connect concurrent goroutines. You can send values into channels from one goroutine and receive those values at another goroutine.
Just think of channel similar to Unix/Linux pipe |
For example :
>ls -al | grep hello
- ls command passing data via pipe |
to grep command.
Some examples of making new channels in Golang :
chint := make(chan int)
chstr := make(chan string)
chitf := make(chan interface{})
channel uses the operator <- to send or receive data.
ch <- v // send v to channel ch.
v := <-ch // receive data from ch, and assign to v
Demo :
package main
import "fmt"
func say(msg chan<- string) {
msg <- "Hello, 世界!"
}
func main() {
message := make(chan string)
go say(message)
fmt.Println(<-message)
}
Output :
Hello, 世界!
Be default, sending and receiving data in channels are blocked. It means that a goroutine expecting data will NOT continue until it receive the data.
So far, we have only see single element channels. Golang has another type of channel that allow more than one element to pass through and it is known as the buffered channel.
The different between buffered channel and normal channel is the n parameter :
ch := make(chan type, n)
To declare a buffered channel, you need to specify the number of elements.
For example :
ch := make(chan bool, 4) // to send/receive 4 boolean elements.
n == 0 ! non-buffer(block)
n > 0 ! buffered channel.(non-block until n elements in the channel)
This code example below demonstrate a buffered channel in action
package main
import "fmt"
func main() {
c := make(chan int, 2) // change n=2 to n=1 will cause deallock error , but not n=3
c <- 1
c <- 2
fmt.Println(<-c)
fmt.Println(<-c)
}
Output :
1
2
if change n to 1
fatal error: all goroutines are asleep - deadlock!
Hope this tutorial will help you to understand better on Golang channels.
SEE ALSO ON CHANNEL DIRECTION TYPE USAGE! at https://www.socketloop.com/references/golang-reflect-chandir-type-example
See also : Golang : Concurrency and goroutine example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+10.3k Golang : Select region of interest with mouse click and crop from image
+15.1k Golang : Find location by IP address and display with Google Map
+5.3k Golang : Stop goroutine without channel
+9.8k Golang : Read file and convert content to string
+10.6k Golang : Get UDP client IP address and differentiate clients by port number
+33.7k Golang : Call a function after some delay(time.Sleep and Tick)
+4.6k Fix Google Analytics Redundant Hostnames problem
+10.8k Golang : Generate random elements without repetition or duplicate
+17.2k Golang : Linked list example
+23.8k Golang : Upload to S3 with official aws-sdk-go package
+19.3k Golang : Example for DSA(Digital Signature Algorithm) package functions
+12.3k Golang : Forwarding a local port to a remote server example