-
Notifications
You must be signed in to change notification settings - Fork 0
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
tanaka0325 / 課題1 #6
base: master
Are you sure you want to change the base?
Conversation
他の方の PR へのレビューを見て気づいたのですが、テスト画像は (追記) |
39388ed
to
5868629
Compare
サブディレクトリの画像も対象にしているのですが、場合によってはハチャメチャな量の画像が対象になってしまう可能性があるので、 |
|
||
## 感想 | ||
|
||
- long option(?) はどうやってやれば良いのだろうか |
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版 みんなのGo言語に、以下の様な形で書かれていました!
const defaultPort = 3000
var (
port string
)
func init() {
flag.StringVar(&port, "port", defaultPort, "port to use")
flag.StringVar(&port, "p", defaultPort, "port to use (short)")
flag.Parse()
}
上記のような冗長な記述を避けたりする等の目的で、サードパーティ製のパッケージも出ている様です。
私はまだ利用したことないのですが、ご参考までに!
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.
おお!!ありがとうございます!!!すごくありがたいです🙏
ab9078e
to
6c9f2bb
Compare
52b060f
to
331d78e
Compare
331d78e
to
a235d16
Compare
package imgconv | ||
|
||
// Args is type for command line arguments. | ||
type Args []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.
imgconvパッケージはコマンドライン前提のパッケージでしょうか?
mainパッケージのみがコマンドラインに依存する形をとったほうが良いです。
var args imgconv.Args | ||
|
||
func init() { | ||
options.From = flag.String("f", "jpg", "file extension before 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.
コマンドラインから貰ってきたデータをそのまま(チェックせずに)設定するのは避けたほうがよいです。
ユーザが入力するデータには予期せぬものもあります。
予期せぬ入力をガードするのはmainパッケージの仕事の方がよいでしょう。
os.Exit(1) | ||
} | ||
|
||
os.Exit(0) |
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の場合は明示的に実行する必要ないです。
Encoder | ||
} | ||
|
||
type CnvImage 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.
この型は何のために定義していますか?
CnvImagePNGなどがCnvImageを継承するイメージであれば、Goには継承は存在しないので不要です。
empty structを基底型した型をさらに基底型とするのはあまり意味がないですね。
// CnvImagePng is type for png format. | ||
type CnvImagePNG CnvImage | ||
|
||
func (ip CnvImagePNG) Decode(r io.Reader) (image.Image, error) { return png.Decode(r) } |
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 newCnvImage(ext string) DecodeEncoder { | ||
switch ext { | ||
case "png": | ||
return &CnvImagePNG{} |
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.
empty struct(struct {})を基底型にしている場合はポインタを取る必要ない(取らないほうが良い)です。
"strings" | ||
) | ||
|
||
var allowedExts = []string{"png", "jpg", "jpeg", "gif", "bmp", "tiff", "tif"} |
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.
mapの方が無駄にforで回らなくて良いかなと思います。
詳細は他の方のレビューコメントをご確認ください。
// convert | ||
for _, path := range paths { | ||
filename := strings.Replace(path, "."+fromExt, "", -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.
これは何かのコメントでしょうか?
コメントは人が読むものなので、不要なものはPushする際には消して置くと良いです。
デバッグ用に残したい場合はその旨をコメントに書くとレビュアーには優しいです。
} | ||
|
||
defer func() { | ||
err = w.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.
名前付き戻り値につかう変数と関数内で使う変数は分けた方が良いです。
return nil, fmt.Errorf("%s is not a directory", n) | ||
} | ||
|
||
if err := filepath.Walk(n, func(path string, info os.FileInfo, err error) 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.
WalkFuncの引数のエラーのエラー処理
func getTargetFilePaths(args Args, from string) ([]string, error) { | ||
uns := args.uniq() | ||
|
||
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.
長さゼロのスライスを作る必要はありません。
var paths []string
でゼロ値のnil
を利用しましょう。
append
は第1引数がtyped nil
でも問題ありません。
} | ||
|
||
func isDir(path string) (bool, error) { | ||
f, err := os.Open(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.
Closeしてないですね
return false, err | ||
} | ||
|
||
fi, err := f.Stat() |
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.Stat
で良いかなと思います。
https://golang.org/pkg/os/#Stat
|
||
// Options is type for command line options. | ||
type Options struct { | ||
From *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.
ポインタにせず、flagパッケージの関数にわたす際に &opt.From のようにポインタを渡すと良いでしょう。
レビューお願いします!
詳しくは README.md を参照ください🙇♂️