Skip to content

Instantly share code, notes, and snippets.

@YuheiNakasaka
Created March 11, 2018 09:36
Show Gist options
  • Save YuheiNakasaka/96fe3098e83ec17e6277eef19807bf2d to your computer and use it in GitHub Desktop.
Save YuheiNakasaka/96fe3098e83ec17e6277eef19807bf2d to your computer and use it in GitHub Desktop.
Goのchannelを使って処理のブロックを実装する
package main
import (
"fmt"
"time"
)
func main() {
fmt.Println("Start")
queue := make(chan int, 3)
go func() {
time.Sleep(5 * time.Second)
n := <-queue
fmt.Printf("pop %d from queue\n", n)
}()
time.Sleep(1 * time.Second)
queue <- 1
fmt.Println("1")
time.Sleep(1 * time.Second)
queue <- 2
fmt.Println("2")
time.Sleep(1 * time.Second)
queue <- 3
fmt.Println("3")
fmt.Println("queue is full")
time.Sleep(1 * time.Second)
queue <- 4
fmt.Println("4")
fmt.Println("End")
}
/* Output:
Start
1
2
3
queue is full
pop 1 from queue
4
End
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment