Go (golang) Tutorials - Concurrency Channels

Published: 10 April 2018
on channel: Ambasoft Java
761
18

#golang #go #concurrency #channels

Concurrency - Channels
------------------------
Pipes through which we can pass data to Goroutines
Send and Receive data from a channel
Whenever a value being read from a channel, Go runtime, will block the execution
until a value gets written to the channel
If there is a read operation from a channel inside a go routine, then there should be
a proper write operation to supply data to the channel, otherwise, go compiler will
raise a panic of deadlock
By default channels are bidirectional

channeldemo.go
--------------
package main
import "fmt"
func main(){
var mychannel chan int //Declaration of variable mychannel as an int channel
if mychannel==nil{
fmt.Println("Channel not initialized")
}
mychannel=make(chan int) //Instantiating or Creating the int channel
fmt.Println(mychannel)

// data:= (arrow operator) mychannel //Read from the channel
// mychannel (arrow operator) data //Writing to the channel
}

newdemo.go
-----------
package main
import "fmt"
func display(mychannel chan int){
fmt.Println("display goroutine")
mychannel(arrow operator)10 //Writing the value 10 to the int channel mychannel
}
func main(){
mychannel:=make(chan int) //Instantiating or Creating an integer channel
go display(mychannel)
num:=(arrow operator)mychannel //Reading data from channel mychannel - Blocks the execution
//until a write happens to the channel
fmt.Println("Inside Main") //Terminate main goroutine
fmt.Println(num)
}


Watch video Go (golang) Tutorials - Concurrency Channels online, duration hours minute second in high quality that is uploaded to the channel Ambasoft Java 10 April 2018. Share the link to the video on social media so that your subscribers and friends will also watch this video. This video clip has been viewed 761 times and liked it 18 visitors.