-
Notifications
You must be signed in to change notification settings - Fork 17
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
base: master
Are you sure you want to change the base?
Conversation
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.
コメントしましたので、参考にしていただければと思います
if err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
} | ||
defer fp.Close() |
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.
deferは実行された時点の構造体などの情報を積んでしまうので、forの中で利用するのは良くない場合があります
deferはfuncのスコープでしか実行されないので、即時関数でforの中身を囲うなどで対処可能です
fmt.Fprintln(os.Stderr, err) | ||
} | ||
defer fp.Close() | ||
io.Copy(fh, fp) |
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.
io.Copy
はエラーを返す可能性があるので、チェックしたほうが良いです
|
||
req, err := http.NewRequest("GET", t, nil) | ||
tr := &http.Transport{} | ||
client := &http.Client{Transport: tr} |
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.
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.
分割ダウンロードを行う