-
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
Kadai2 shinta #17
base: master
Are you sure you want to change the base?
Kadai2 shinta #17
Conversation
allowExtList := []string{"jpg", "jpeg", "png", "gif"} | ||
allowExtMap := map[string]bool{} | ||
for _, ext := range allowExtList { | ||
allowExtMap[ext] = true |
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.
これで良さそうです。
allowExtMap := map[string]bool{
"jpg": true,
"jpeg": true,
"png": true,
"gif": true,
}
return nil | ||
} | ||
|
||
func (a *arg) convertExt() { |
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.
何回も呼ぶと何回も.
がつきますね。
} | ||
|
||
// createImgStrunct は、imageFile structを生成し、返します。 | ||
func createImgStruct(path string) (image imageFile) { |
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 convertExec(path, afterExt string) error { | ||
img := createImgStruct(path) | ||
targetImg, err := os.Open(img.path + "/" + img.base + img.ext) |
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.Join
を使う
if err != nil { | ||
return err | ||
} | ||
outputImg, err := os.Create((img.path + "/" + img.base + afterExt)) |
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.
上記と同じ
default: | ||
png.Encode(outputImg, readImg) | ||
} | ||
err = targetImg.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.
下の代入でかき消される
func TestValid(t *testing.T) { | ||
t.Helper() | ||
for _, arg := range exampleArgs { | ||
t.Run(arg.testCase, func(t *testing.T) { |
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.
t.Run
を呼び出すのはヘルパーとは言えない
} | ||
|
||
func TestValid(t *testing.T) { | ||
t.Helper() |
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.
ヘルパーではない
afterExt string | ||
} | ||
|
||
var exampleArgs = []exampleArg{ |
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.
テストデータは極力テスト関数の中で定義する
kadai2/shinta/main.go
Outdated
p := flag.String("p", "jpeg", "変換前拡張子") | ||
a := flag.String("a", "png", "変換後拡張子") | ||
if flag.Parse(); flag.Parsed() { | ||
dir, preExt, afterExt = *d, *p, *a |
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.StringVar
でdir
とかのポインタを渡した方が良さそう
課題2
io.Readerとio.Writerについて調べてみよう
標準パッケージでどのように使われているか
io.Readerとio.Writerがあることでどういう利点があるのか具体例を挙げて考えてみる
test