Advertisement
In this blog, we will learn about Golang Channel Syntax.
Let us see what we will be learning in this Golang Channel Syntax blog.
- Golang Channels Basics
- Restricted Data Flow in Golang Channels
- Send Only Channel
- Receive only Channel
- Golang Buffered Channels
- Iterating over Channels in Golang and Closing Channels in Golang(for … range loops)
- Select Statements in Golang
Before learning Golang Channels make sure the Basics of Golang are clear:
- Variables in Golang
- Datatypes and Operations in Golang
- Constants in Golang
- Golang Arrays and Slices
- Go Maps and Structs
- Golang If-else and Switch Statements
- Go for loop
- Golang Defer, Panic and Recover
Learn Golang Advanced Topics:
Golang Channels Explanation
In the previous blog, we learned about Golang Goroutines that gave us the power to make our application concurrent, but we were facing problems in the synchronization of the goroutines.
Channels in Golang help us to synchronize the goroutines, so channels are mostly used with goroutines.
If you don’t know about Golang Goroutines, must-read from the blog.
Golang Channel Syntax
Syntax:
make ( chan )
The make function in Golang is used to create almost every data Structure. Channels in Golang can also be created using the make function.
While creating a channel in the Go language, the make function takes the chan keyword and the channel datatype.
This states that the channels in Golang are also strongly typed, as we have to define the datatype at the creation of a channel.
Example:
ch := make(chan int)
Golang Channel Example:
package main
import (
"fmt"
"sync"
)
var wg sync.WaitGroup = sync.WaitGroup{}
func main() {
ch := make(chan int)
wg.Add(2)
go func() {
i := <-ch
fmt.Println("Value of Channel i =", i)
wg.Done()
}()
go func() {
ch <- 17
wg.Done()
}()
wg.Wait()
}
Output:
Value of Channel i = 17
There are two goroutines in the example, one is for the sending value to channel and the other is to receive value from the channel.
Sending Value to the channel
When the arrow is towards Channel name in Golang, that is referred to as sending values to Golang Channels.
Example:
// This is Anonymous Goroutine to send value to channel
go func() {
ch <- 17
wg.Done()
}()
Receiving Value from Channel
When the arrow is pointing towards the variable name, that is referred to as receiving value or taking value from the channel in Golang.
Example:
go func() {
i := <-ch
fmt.Println("Value of Channel i =", i)
wg.Done()
}()
Value Manipulation in Golang Channels
When a Channel is assigned value it takes a copy of the value and doesn’t affect the channel if the value is later manipulated.
Example:
go func() {
var i int
i = 17
ch <- i
i = 13
wg.Done()
}()
Output remains the same as above. The Value of the channel is not affected.
Golang Global Channel Syntax
When the Golang Channel is declared at the Global level, then all the functions can use that channel to send or receive data.
At the Global level, the declaration of channel changes as the short declaration is not possible at the global level declaration.
Example:
// Global Channel
var ch = make(chan int, 50)
func send() {
ch <- 17
}
func main() {
send()
fmt.Println(<-ch)
}
Output:
17
Also, read Why Golang is called the future of Server-side language?
Learn more about Channels in Golang from the official Documentation.