Go (golang) Tutorials - Concurrency Channels

Опубликовано: 10 Апрель 2018
на канале: 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)
}


Смотрите видео Go (golang) Tutorials - Concurrency Channels онлайн, длительностью часов минут секунд в хорошем качестве, которое загружено на канал Ambasoft Java 10 Апрель 2018. Делитесь ссылкой на видео в социальных сетях, чтобы ваши подписчики и друзья так же посмотрели это видео. Данный видеоклип посмотрели 761 раз и оно понравилось 18 посетителям.