Skip to content

[WIP] 第11回Go勉強会の課題@luccafort #59

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

luccafort
Copy link

課題

https://mf.esa.io/posts/132902

  • channel/basic を実装する
    • Goroutineを使う
    • Channelを使う
  • Sleep SortをChannelで実装する
    • <-Channelを実装する
    • for range Channel を実装する
    • close(Channel) を実装する
  • time.Tickerを用いた定期実行
    • Selectを使う
    • 何かしらの処理を定期的に行う
    • 定期的な処理を終了させてmain関数を終了させる。(終了条件は好き好き)

main関数内でchannel/basic/main.goの実装内容をGoroutineとChannelを使って実装
変更点

- Channelが返す型はStringからIntegerに変更
- Goroutineで実行する無名関数の引数として任意の数値を指定するように変更
受信専用チャンネルを使ってSleepSortを実装
受信専用チャンネルではclose(channel)できなかったので関数を2つにわけて実装する苦肉の策
SleepSortを行う関数を実装、WaitGroupで処理の同期を実装(でも引数にチャンネル取るのがダサい)
Copy link
Author

@luccafort luccafort left a comment

Choose a reason for hiding this comment

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

まだ課題3ができてないけど途中経過として報告。

return ch
}

func sleepSortCh(ch chan int, nums []int) chan int {
Copy link
Author

Choose a reason for hiding this comment

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

引数にチャンネルを取ってるが依存してしまっているので依存しないようにしたいが考えつかなかった。

count := len(nums)
ch := make(chan int, count)
go func(nums []int) {
defer close(ch)
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() するようにした……がもうちょいなんかやりようがある気がする。

}

func sleepSortCh(ch chan int, nums []int) chan int {
var wg sync.WaitGroup
Copy link
Author

Choose a reason for hiding this comment

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

WaitGroupで実装したがerrgroup.Groupで実装したほうがよかったかも。

Copy link
Author

Choose a reason for hiding this comment

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

通常こういう処理を行うときって外部APIへの通信だったりするのでそのレスポンスを待ち受けるならエラーが返る可能性を考慮して errgroup.Group じゃないと駄目そう。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant