Summary of Goroutines and Channels

Summary of Goroutines and Channels

  • Attention:This chapter is a little difficult at the first time to learn.

The book I have read of this chapter:

  • Head First Go

What is goroutines:

  • Goroutines are functions that are run concurrently.

How to new a goroutines?

  • It is very easy.New goroutines are started with a go statement:an ordinary function call preceded by the “go” keyworld.

Example:

go myFunction()
go otherFunction("argument")

Every Go program runs at least one goroutine,named main routine(the routine where the main function is started )

Using goroutines

Suppose we have two functions now named a() and b(),now look at the code below,we will run them in different ways.

  • without keyword:go,both the func a and b are run in the same routine:main routine
    func main(){
        a()
        b()
        fmt.Println("end main()")
    }
    
  • Using keyword go,the func a and b will run at their own routine,they can run concurrently.
    func main(){
        go a()
        go b()
        fmt.Println("end main()")
    } 
    
    

Go programs stop running as soon as the main goroutine ends,even if other goroutines are still running

But there are still some problems:

  • in the program above,the go program will go to an end,and the func a and b have no chance to run,because the main func go to an end quickly,thus making the main goroutine finish.
  • We can’t get return value from the go func
     size = go responseSize("https://example.com/")//You are saying "go run this;I am not going to wait.
     fmt.Println(size)//but then what is the return value?
    

Therefore,we need another feature of Go called channels.

Channels:a way to communicate between goroutines

Not only do channels allow you to send values from one goroutine to another,they ensure the sending routine has send the value before the accepting routine attempts to use it.

Things we need to demonstrate channels:

  1. Create a channel
  2. Write a func that receive a channel as a parameter.We will run this func at a separate goroutine,and use it to send values over the channel.
  3. Receive the sent values in our original goroutine.

How to declare and create a channel?

var myChannel chan int//Type of values the channel will carry
myChannel = make(chan int)

or:

myChannel:=make(chan int)

How to send and receive values with channel?

myChannel <- 3.14//send the value to the channel
<- myChannel //receive the value from the channel

Syschronizing goroutine with channel

We mentioned that channels also ensure the sending goroutine has sent the value before the receiving goroutine attempt to use it.
Channels do that by pausing all further operations in the current goroutine.A send operation block the sending goroutine until the receiving goroutine executes a receive operation.And vice versa.

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值