-
Notifications
You must be signed in to change notification settings - Fork 2
Kadai2-hisamura #15
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?
Kadai2-hisamura #15
Conversation
b190222 to
de46855
Compare
de46855 to
14db38c
Compare
38cbcf3 to
ce65e82
Compare
| return "", err | ||
| } | ||
| convertedName = dstFile.Name() | ||
| defer dstFile.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の中で呼び出さない。
関数に分けることで避けることができる。
| } | ||
|
|
||
| //指定したディレクトリ配下のファイルを取得 | ||
| func dirwalk(dir string) []string { |
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.
filepath.Walkを使わない理由は?
| func dirwalk(dir string) []string { | ||
| files, err := ioutil.ReadDir(dir) | ||
| if err != nil { | ||
| panic(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.
panicは使わない。
プロダクトを開発している際に使うことは殆どない。
| panic(err) | ||
| } | ||
|
|
||
| var paths []string |
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.
👍
| @@ -0,0 +1,70 @@ | |||
| package convert | |||
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.
パッケージを分ける(convert_testにする)
| package main | ||
|
|
||
| import ( | ||
| "dojo6/kadai2/hisamura/convert" |
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.
標準パッケージ以外は空行を分けることが多い。
その際、サードパティパッケージが下になる。
| convertFile, err := convert.Convert(flagOps) | ||
|
|
||
| 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に出す。
| } | ||
|
|
||
| func validation(flagOps convert.FlagOps) bool { | ||
| if flagOps.Dest != "png" && flagOps.Dest != "jpg" && flagOps.Dest != "jpeg" && flagOps.Dest != "gif" { |
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の条件をそのままreturnしても良さそう
標準パッケージでどのように使われているか
fmt.Fprintでは以下のように記述されている。
同じファイル内のFprint内での呼び出しは以下。
io.writerのインターフェースはwriteをメソッドに持っている型なら引数に設定できるので、os.Stdoutを引数に指定し、呼び出している。
io.Readerとio.Writerがあることでどういう利点があるのか具体例を挙げて考えてみる
コードが簡潔にできるために使用することができる。
interfaceがないと、それぞれの型を引数に指定し、複数の関数を作らなくてはいけない。
それがinterfaceを設定できることで、interfaceの関数は使用できることが約束される。
io.Readerの型を満たしているということはReadメソッドが使用できることが約束されており、引数にio.Readerを指定することで関数内でその引数の型の違いを気にすることなく、Readメソッドを使うことができ、シンプルにコードを記述することができる。