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 sminamot #43

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

Conversation

sminamot
Copy link

分割ダウンロードを行う

  • Rangeアクセスを用いる
  • いくつかのゴルーチンでダウンロードしてマージする
  • エラー処理を工夫する
  • キャンセルが発生した場合の実装を行う

Copy link

@mizkei mizkei left a comment

Choose a reason for hiding this comment

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

コメントしましたので、参考にしていただければと思います

if err != nil {
fmt.Fprintln(os.Stderr, err)
}
defer fp.Close()
Copy link

Choose a reason for hiding this comment

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

deferは実行された時点の構造体などの情報を積んでしまうので、forの中で利用するのは良くない場合があります
deferはfuncのスコープでしか実行されないので、即時関数でforの中身を囲うなどで対処可能です

fmt.Fprintln(os.Stderr, err)
}
defer fp.Close()
io.Copy(fh, fp)
Copy link

Choose a reason for hiding this comment

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

io.Copy はエラーを返す可能性があるので、チェックしたほうが良いです


req, err := http.NewRequest("GET", t, nil)
tr := &http.Transport{}
client := &http.Client{Transport: tr}
Copy link

Choose a reason for hiding this comment

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

http.Clientはsafe for concurrentですので、どこかで一つつくっておくだけでも十分です
また、Transportはnilであった場合、DefaultTransportが利用されるので、 client := new(http.Client) だけでも問題なく利用できます

https://golang.org/pkg/net/http/#Client

   // Transport specifies the mechanism by which individual
   // HTTP requests are made.
   // If nil, DefaultTransport is used.
   Transport RoundTripper

https://github.com/gopherdojo/dojo4/pull/43/files#diff-721da0c5f369336fd01ab4ac84891ea0R144
上記でCancelRequestを呼ぶために生成しているのかもしれませんが、すでにdeprecatedですので、WithContextを利用したほうが良いです
https://golang.org/pkg/net/http/#Transport.CancelRequest

Deprecated: Use Request.WithContext to create a request with a cancelable context instead.

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.

2 participants