Skip to content

Commit

Permalink
feat: add implementation of buffered channel
Browse files Browse the repository at this point in the history
Yuki San committed Mar 15, 2023
1 parent c91adfb commit 51b9431
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -3,5 +3,5 @@ package main
import "explore-go/test"

func main() {
test.Channel()
test.BufferedChannel()
}
28 changes: 28 additions & 0 deletions test/buffered_channel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package test

import (
"fmt"
"runtime"
)

func BufferedChannel() {
runtime.GOMAXPROCS(2)

messages := make(chan int, 3)

go func() {
for {
i := <-messages
fmt.Println("receive data", i)

}
}()
for i := 0; i < 5; i++ {
fmt.Println("send data", i)
messages <- i

}

// kode dibawah berperan untuk membiarkan data terakhir agar diterima oleh goroutine
// time.Sleep(5 * time.Second)
}

0 comments on commit 51b9431

Please sign in to comment.