mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 05:33:04 +08:00
update book code
This commit is contained in:
29
eBook/examples/chapter_14/goroutine3.go
Normal file
29
eBook/examples/chapter_14/goroutine3.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
ch := make(chan string)
|
||||
go sendData(ch)
|
||||
getData(ch)
|
||||
}
|
||||
|
||||
func sendData(ch chan string) {
|
||||
ch <- "Washington"
|
||||
ch <- "Tripoli"
|
||||
ch <- "London"
|
||||
ch <- "Beijing"
|
||||
ch <- "Tokio"
|
||||
close(ch)
|
||||
}
|
||||
|
||||
func getData(ch chan string) {
|
||||
for {
|
||||
input, open := <-ch
|
||||
if !open {
|
||||
break
|
||||
}
|
||||
fmt.Printf("%s ", input)
|
||||
}
|
||||
}
|
||||
// Washington Tripoli London Beijing Tokio
|
Reference in New Issue
Block a user