Skip to content

第11回の課題(大倉) #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion channel/task/basic/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
package main

// channel/basicのInt版
import (
"fmt"
"math/rand"
"time"
)

// Goroutine内で好きなIntをchannelを経由でMain Goroutineに渡し標準出力にPrintしてみる。
func main() {
ch := make(chan int)

go func() {
defer close(ch)

rand.Seed(time.Now().Unix())
max := rand.Intn(10)

for i := 0; i < max; i++ {
time.Sleep(1 * time.Second)
ch <- rand.Intn(100)
}
}()

for v := range ch {
fmt.Printf("revieved: %d\n", v)
}
}
31 changes: 31 additions & 0 deletions channel/task/sleep_sort/main.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,35 @@
package main

import (
"fmt"
"time"
)

func main() {
nums := []int{1, 3, 2, 5, 4}

sortedNumbersChan := sortedNumbersChan(nums)

for n := range sortedNumbersChan {
fmt.Println(n)
}
}

func sortedNumbersChan(nums []int) <-chan int {
ch := make(chan int)

for _, num := range nums {
isLast := num == len(nums)

go func(n int, isLast bool) {
if isLast {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

メモ:最後に完了するgoroutineをどうやって判別するのかわからなかったので、もう一度考える

defer close(ch)
}

time.Sleep(time.Duration(n) * time.Second)
ch <- n
}(num, isLast)
}

return ch
}
39 changes: 39 additions & 0 deletions channel/task/ticker/main.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,43 @@
package main

import (
"fmt"
"time"
)

func main() {
fmt.Println("+++++ Metronome is start +++++")

bpm := 120
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

標準入力から受け取れるようにする


// metronomeCh := metronomeChan(120)
doneCh := make(chan interface{})

waitSpan := time.Duration(60 * 1000 / bpm)
flag := 0
ticker := time.NewTicker(waitSpan * time.Millisecond)
defer ticker.Stop()

go func() {
time.Sleep(5 * time.Second)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

標準入力から止めれるようにしてみたい

doneCh <- "5 second timeout"
}()

loop:
for {
select {
case <-ticker.C:
if flag == 0 {
fmt.Println("tic")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

beepとかならせるかな・・?

flag = 1
} else {
fmt.Println("tac")
flag = 0
}
case v := <-doneCh:
fmt.Println(v)
break loop
}
}
fmt.Println("+++++ Metronome is end +++++")
}