-
Notifications
You must be signed in to change notification settings - Fork 0
Kadai1 segakazzz #1
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/segakazzz/.gitignore
Outdated
@@ -0,0 +1,7 @@ | |||
.idea | |||
image/in/* |
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.
テスト用のデータはtestdata以下に置くと良いです。
kadai1/segakazzz/imgconv/imgconv.go
Outdated
dir = flag.String("d", ".", "Indicate directory to convert") | ||
in = flag.String("i", "jpg", "Indicate input image file's extension") | ||
out = flag.String("o", "png", "Indicate output image file's extension") | ||
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.
ここで定義しなくても、36行目で定義されます。
kadai1/segakazzz/imgconv/imgconv.go
Outdated
flag.Parse() | ||
c, err := newConverter(*dir, *in, *out) | ||
if err != nil { | ||
log.Fatal(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.
log.Fatal
は内部で、os.Exit(1)
を呼んでいます。
そのため、終了コードを自分で決められないので、fmt.Fprintln(os.Stderr, ....)
とos.Exit
を自分で呼んだ方が良いです。
kadai1/segakazzz/imgconv/imgconv.go
Outdated
case "jpg", "png": | ||
input = strings.ToLower(input) | ||
default: | ||
return &converter{}, fmt.Errorf("Input extension is not valid. Select one from jpg/png") |
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.
エラーの際は最後の戻り値以外はゼロ値(この場合はポインタなのでnil
)を返す。
kadai1/segakazzz/imgconv/imgconv.go
Outdated
} | ||
|
||
// Convert method converts all jpg files in dirname to png. "out" folder is generated if it doesn't exist. | ||
func (c *converter) Convert() (e 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.
名前付き戻り値は必要な時以外は使わない
kadai1/segakazzz/imgconv/imgconv.go
Outdated
func (c *converter) getSourceFiles() ([]os.FileInfo, error) { | ||
files, err := ioutil.ReadDir(c.dirname) | ||
if err != nil { | ||
return []os.FileInfo{}, 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.
return nil, err
return nil | ||
} | ||
|
||
func (c *converter) convertSingle(filename string) (e 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.
何をreturn
したか追いづらいので、名前付き戻り値はできるだけ使わない。
func (c *converter) convertSingle(filename string) (e error) { | ||
input := filepath.Join(c.dirname, filename) | ||
outDir := filepath.Join(c.dirname, "out") | ||
output := filepath.Join(outDir, strings.Replace(strings.ToLower(filename), "."+c.input, "."+c.output, -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.
Replace
まで使わなくてもスライス演算でできます。
kadai1/segakazzz/imgconv/imgconv.go
Outdated
os.Mkdir(outDir, 0755) | ||
} | ||
|
||
in, _ := os.Open(input) |
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/segakazzz/imgconv/imgconv.go
Outdated
return | ||
} | ||
} | ||
defer in.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.
out
を開く前にdefer
を仕掛けておく
kadai1/segakazzz/imgconv/imgconv.go
Outdated
} | ||
} | ||
defer in.Close() | ||
defer out.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.
Create
なのでClose
する際にエラー処理もする。
課題1のレビューをお願い致します。
main packageでどこまで記述して、自作パッケージにどこまでの内容を切り出すべきか判断がよくわかっていないので、良い書き方があればコメント頂けると幸いです。
その他はkadai1/segakazzz/README.mdをご参照ください。