-
Notifications
You must be signed in to change notification settings - Fork 6
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
【課題3】タイピングゲームと分割ダウンローダー #31
base: master
Are you sure you want to change the base?
Conversation
errgroup "golang.org/x/sync/errgroup" | ||
) | ||
|
||
var COUNT = 4 |
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.
定数であれば、constで先頭を大文字にするとパッケージ外に公開されます
|
||
func main() { | ||
|
||
url := "https://misc.laboradian.com/test/003" |
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.
const?
header, err := HeaderInfo(url) | ||
|
||
if err != nil { | ||
fmt.Println(err) |
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.
エラーであれば、os.Stderrに出して、os.Exit(1)とかで終了する
} | ||
|
||
func HeaderInfo(url string) (map[string][]string, error) { | ||
req, _ := http.NewRequest("HEAD", "https://misc.laboradian.com/test/003/", nil) |
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.
エラー処理
func HeaderInfo(url string) (map[string][]string, error) { | ||
req, _ := http.NewRequest("HEAD", "https://misc.laboradian.com/test/003/", nil) | ||
|
||
client := new(http.Client) |
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.DefaultClient
で十分。
defer resp.Body.Close() | ||
|
||
if err != nil { | ||
fmt.Printf("ioutil err: %v", err) |
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.
エラーの表示も立派なエラーハンドリングなので、2重にハンドリングしない。
表示するなら表示する、returnするならreturnする。
return resp.Header, nil | ||
} | ||
|
||
func canRangeAccess(header map[string][]string) bool { |
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.Headerを使った方が良さそう
func canRangeAccess(header map[string][]string) bool { | ||
v, ok := header["Accept-Ranges"] | ||
|
||
if ok { |
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.
早めにreturnしたほうがよい
|
||
func Download(url, byteRange string) error { | ||
|
||
req, _ := http.NewRequest("GET", url, nil) |
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.
error処理
|
||
client := new(http.Client) | ||
resp, err := client.Do(req) | ||
// TODO: エラー処理する? |
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.
エラー処理は必須です
_, fileName := path.Split(url) | ||
fmt.Println(url, byteRange, fileName) | ||
|
||
file, err := os.Create(path.Join("./tmp/", fileName+"_"+byteRange)) |
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.
ファイルにpathパッケージは使わない。filepathを使う。
一時ファイルはioutilとかを使う https://golang.org/pkg/io/ioutil/#TempDir
} | ||
_, err = io.Copy(file, resp.Body) | ||
if closeErr := file.Close(); err == nil { | ||
err = closeErr |
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.
そのままreturnしないのはなぜ?
} | ||
|
||
func NthRange(length, parallel_num, n int) string { | ||
bytes_per_file := length / parallel_num |
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.
識別子(変数名など)はスネークケースを使わない。キャメルケースを使う。
func NthRange(length, parallel_num, n int) string { | ||
bytes_per_file := length / parallel_num | ||
if n == 1 { | ||
return "0-" + strconv.Itoa(bytes_per_file) |
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.
returnするならelseやelse if はいらない
if n == 1 { | ||
return "0-" + strconv.Itoa(bytes_per_file) | ||
} else if n < parallel_num { | ||
return strconv.Itoa(bytes_per_file*(n-1)+1) + "-" + strconv.Itoa(bytes_per_file*n) |
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.
fmt.Sprintfを使ったほうがわかりやすそう
readBytes, err := ioutil.ReadAll(f) | ||
writeBytes = append(writeBytes, readBytes) | ||
|
||
if err := f.Close(); err != nil { |
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.
Writeじゃないのでエラー処理は不要ではある。
|
||
fmt.Println(fileName) | ||
|
||
err := ioutil.WriteFile(fileName, bytes.Join(writeBytes, emptyByte), 0644) |
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.
こうすると一気にメモリを消費するので、徐々に書き込むべき。
|
||
words, err := RegisterWords() | ||
if err != nil { | ||
fmt.Println("err") |
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.
エラーメッセージは丁寧に書く。
os.Stderrに出す。
課題3
1.タイピングゲームを作ろう
2.分割ダウンローダを作ろう