-
Notifications
You must be signed in to change notification settings - Fork 25
[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
base: master
Are you sure you want to change the base?
Conversation
main関数内でchannel/basic/main.goの実装内容をGoroutineとChannelを使って実装 変更点 - Channelが返す型はStringからIntegerに変更 - Goroutineで実行する無名関数の引数として任意の数値を指定するように変更
受信専用チャンネルを使ってSleepSortを実装 受信専用チャンネルではclose(channel)できなかったので関数を2つにわけて実装する苦肉の策 SleepSortを行う関数を実装、WaitGroupで処理の同期を実装(でも引数にチャンネル取るのがダサい)
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.
まだ課題3ができてないけど途中経過として報告。
return ch | ||
} | ||
|
||
func sleepSortCh(ch chan int, nums []int) chan int { |
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.
引数にチャンネルを取ってるが依存してしまっているので依存しないようにしたいが考えつかなかった。
count := len(nums) | ||
ch := make(chan int, count) | ||
go func(nums []int) { | ||
defer close(ch) |
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するところで defer close()
するようにした……がもうちょいなんかやりようがある気がする。
} | ||
|
||
func sleepSortCh(ch chan int, nums []int) chan int { | ||
var wg sync.WaitGroup |
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.
WaitGroupで実装したがerrgroup.Group
で実装したほうがよかったかも。
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.
通常こういう処理を行うときって外部APIへの通信だったりするのでそのレスポンスを待ち受けるならエラーが返る可能性を考慮して errgroup.Group
じゃないと駄目そう。
課題
https://mf.esa.io/posts/132902
channel/basic
を実装する<-Channel
を実装するfor range Channel
を実装するclose(Channel)
を実装する