Skip to content
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

Kadai3 2 gosagawa #58

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Kadai3 2 gosagawa #58

wants to merge 4 commits into from

Conversation

gosagawa
Copy link

@gosagawa gosagawa commented Sep 9, 2018

自力で作りきるのが厳しく、他の方の実装を参考にしつつ最低限ダウンロードまではできるように実装しました。
キャンセルが発生した場合の実装はできていません。
引き続きブラッシュアップしますが、現状の状態で申請いたします。お願いします。

@vvakame
Copy link

vvakame commented Sep 10, 2018

dep 使ってる偉い…!!

Copy link

@vvakame vvakame left a comment

Choose a reason for hiding this comment

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

自力で作りきるのが厳しかった理由はご自身の中で明確でしょうか?
明確であれば後は練習するだけですが、そうでなければ誰かに相談してみるのも良い方法だと思います。


func main() {

url := os.Args[1]
Copy link

Choose a reason for hiding this comment

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

この後に TODO check args とあるので自覚されている気もしますが、どのような引数が与えられた(与えられなかった)場合でもpanicにならないようにしましょう。


d, err := NewDownloader(url)
if err != nil {
fmt.Printf("\ndownload initialize error. %v", err)
Copy link

Choose a reason for hiding this comment

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

通常、お尻に \n したほうがよいと思います

return http.DefaultClient.Do(req)
}

func getFileName(resourceUrl string) string {
Copy link

Choose a reason for hiding this comment

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

自力で文字列操作するよりも、既存のものが既に準備されていないか調べるようにしましょう。
go で path を操作したいので、go path と検索してみます。
すると、 https://golang.org/pkg/path/ が見つかり、準備されている関数を調べていくと https://golang.org/pkg/path/#Base が妥当そうだな、とわかります。

return nil, err
}

size := uint(res.ContentLength)
Copy link

Choose a reason for hiding this comment

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

uint を使うのは賢いですね!

return d, nil
}

func NewWorker(d *Downloader, size uint, i uint, split uint, url string) (*worker, error) {
Copy link

Choose a reason for hiding this comment

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

この関数はpackage外部にexportするべきかどうか考えてみましょう(しなくてよさそう

bytesToFinishReading = size
}
w := &worker{
processId: i,
Copy link

Choose a reason for hiding this comment

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

プロセスIDと言われると別のものを想像するので、単に idindex が妥当な名前かもしれません

if err != nil {
return errors.Wrap(err, "failed to open part file")
}
io.Copy(outputFile, partFile)
Copy link

Choose a reason for hiding this comment

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

io.Copy はerrを返すので、無視せずにちゃんとチェックするようにしましょう。
https://golang.org/pkg/io/#Copy

errを無視している場合、それを怒ってくれるツールがあった気がします( go vet だっけ…?
いちいち調べるのがめんどくさい場合、そういうツールの助けを得るのはいい考えです

return errors.Wrap(err, "failed to open part file")
}
io.Copy(outputFile, partFile)
partFile.Close()
Copy link

Choose a reason for hiding this comment

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

Openした後に defer partFile.Close() とすることで、Close漏れを防ぐことができます。
"正しく処理する" ことを努力するより、 "嫌でも正しくなってしまう" 習慣を身につけるほうが後々楽ができます。

@gosagawa
Copy link
Author

@vvakame レビューどうもありがとうございます!
自分でやるのを諦めた理由は自分の中で十分にトライアンドエラーをする時間を確保でき図、気づいたら日曜日の夜だったためです。まだ書くのに時間がかかるので、これくらいのプログラムをどのくらいの時間でみなさんが作れるのかが気になってます。是非次回の懇親会にでもやった皆様と経験者の方に聞いてみたいと思います。
全体的な方針がつかめず最初の一歩に詰まる場合は躊躇なく聞こうと思います。

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

Successfully merging this pull request may close these issues.

None yet

2 participants