Skip to content

Instantly share code, notes, and snippets.

@aggresss
Created April 24, 2018 06:19
Show Gist options
  • Save aggresss/dc525d6fb75fa2af4b7b11b055ceb689 to your computer and use it in GitHub Desktop.
Save aggresss/dc525d6fb75fa2af4b7b11b055ceb689 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"time"
)
func main() {
c := make(chan int)
go send(c)
go recv(c)
time.Sleep(30 * time.Second)
}
//只能向chan里写数据
func send(c chan<- int) {
for i := 0; i < 10; i++ {
time.Sleep(1 * time.Second)
c <- i
}
}
//只能取channel中的数据
func recv(c <-chan int) {
for i := range c {
fmt.Println(i)
}
}
@aggresss
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment