-
Notifications
You must be signed in to change notification settings - Fork 6
【課題1】画像変換コマンド作成 #2
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?
Conversation
kadai1/su-san/image/image.go
Outdated
"path/filepath" | ||
) | ||
|
||
var supportedFormats = map[string]int{"jpg": 1, "jpeg": 1, "png": 1, "gif": 1} |
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.
値はint
じゃなくて、bool
の方が便利です(ゼロ値がfalse
なのでキーが無ければfalse
になります)
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.
ok
で判断する場合はstruct{}
の方が軽い
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.
int型に意味はないためbool型に修正しました。
intだと1以外の数値があると推測されるため。
kadai1/su-san/image/image.go
Outdated
} | ||
|
||
// SupportedFormats は指定フォーマットが対応しているか確認するメソッドです | ||
func (c ConvExts) SupportedFormats() 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.
メソッドのレシーバは構造体の場合は基本的にはポインタにする
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.
構造体の中身を更新しない場合はポインタにしなくてもよいと思ってましたが、
コピーが発生するため基本的にポインタの方が良いのですね。
修正しました!
} | ||
|
||
// FmtConv は指定されたフォーマットからフォーマットへ変換する関数です | ||
func FmtConv(path string, exts ConvExts) (err error) { |
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.
特別な用途が無い限りは、名前付き戻り値はあまり使わない
- 名前付き戻り値でしかできない場合
- 戻り値が(型で)分かりづらい場合にドキュメント的に
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.
直しました!
// FmtConv は指定されたフォーマットからフォーマットへ変換する関数です | ||
func FmtConv(path string, exts ConvExts) (err error) { | ||
pathWithoutExt := path[:len(path)-len(filepath.Ext(path))+1] | ||
ext := filepath.Ext(path)[1:] |
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.
拡張子がない場合は?
kadai1/su-san/image/image.go
Outdated
} | ||
|
||
outputFile, err := os.Create(pathWithoutExt + exts.outExt) | ||
defer outputFile.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.
Closeより、errのチェックを先にする
書き込みの場合はCloseのエラー処理をする。
kadai1/su-san/image/image.go
Outdated
encodeErr = gif.Encode(outputFile, img, nil) | ||
} | ||
|
||
if encodeErr == 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.
即座にreturnしたほうがわかりやすい。
if encodeErr != nil {
return encodeErr
}
kadai1/su-san/image/image.go
Outdated
} | ||
|
||
if encodeErr == nil { | ||
err = os.Remove(path) |
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.
結局err
に代入しても意味がない。
こうするなら
err = os.Remove(path)
return
としなければならない
kadai1/su-san/main.go
Outdated
import ( | ||
"flag" | ||
"fmt" | ||
//"io/ioutil" |
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.
コメントアウトせずにgoimports
を使う。
PR投げる際には意図のあるコメント以外は書かない
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.
失礼しました。修正しました!
kadai1/su-san/main.go
Outdated
|
||
convExts := image.NewConvExts(*inputExt, *outputExt) | ||
if convExts.SupportedFormats() == false { | ||
fmt.Println("unsupported format! please specify these format [png jpg jpeg 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.
エラーならos.Stderr
に出す
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.
修正しました!
kadai1/su-san/main.go
Outdated
} | ||
|
||
convExts := image.NewConvExts(*inputExt, *outputExt) | ||
if convExts.SupportedFormats() == false { |
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 !convExts.SupportedFormats()
と書く
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.
どちらが一般的な書き方かわかっていませんでした。
修正しました。
kadai1/su-san/main.go
Outdated
return | ||
} | ||
|
||
targetFiles := []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.
ゼロ値で大丈夫。append
は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.
修正しました。
やったこと
進捗
一日経って見返して下記改善したい。
特に見てもらいたい点