-
Notifications
You must be signed in to change notification settings - Fork 25
第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
base: master
Are you sure you want to change the base?
第11回の課題(大倉) #57
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
} | ||
} |
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 { | ||
defer close(ch) | ||
} | ||
|
||
time.Sleep(time.Duration(n) * time.Second) | ||
ch <- n | ||
}(num, isLast) | ||
} | ||
|
||
return ch | ||
} |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 +++++") | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
メモ:最後に完了するgoroutineをどうやって判別するのかわからなかったので、もう一度考える