使用channel实现goroutine

使用channel实现goroutine

package main

import (
    "fmt"
    "time"
)

var message = make(chan string)

//往channel中输入信息
func sample1()  {
    message <- "hello gorotine."
}

//消费channel中的信息
func sample2()  {
    str := <- message
    str = str + " run fast,run the world."
    message <- str

}

func main()  {
    go sample1()
    go sample2()
    time.Sleep(time.Second)
    fmt.Println(<-message)
    fmt.Println("message")

}

猜你喜欢

转载自blog.51cto.com/huwho/2334813