Skip to content

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Kadai2-hisamura #15

wants to merge 1 commit into from

Conversation

hisamura333
Copy link

@hisamura333 hisamura333 commented Jul 28, 2019

標準パッケージでどのように使われているか

fmt.Fprintでは以下のように記述されている。

func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error) {  
   p := newPrinter()  
   p.doPrintf(format, a)  
   n, err = w.Write(p.buf)  
   p.free()  
   return  
}

同じファイル内のFprint内での呼び出しは以下。

Fprintf(os.Stdout, format, a...)

io.writerのインターフェースはwriteをメソッドに持っている型なら引数に設定できるので、os.Stdoutを引数に指定し、呼び出している。

io.Readerとio.Writerがあることでどういう利点があるのか具体例を挙げて考えてみる

コードが簡潔にできるために使用することができる。
interfaceがないと、それぞれの型を引数に指定し、複数の関数を作らなくてはいけない。
それがinterfaceを設定できることで、interfaceの関数は使用できることが約束される。
io.Readerの型を満たしているということはReadメソッドが使用できることが約束されており、引数にio.Readerを指定することで関数内でその引数の型の違いを気にすることなく、Readメソッドを使うことができ、シンプルにコードを記述することができる。

@hisamura333 hisamura333 changed the title [WIP]Kadai2 hisamura Kadai2-hisamura Jul 29, 2019
return "", err
}
convertedName = dstFile.Name()
defer dstFile.Close()
Copy link
Member

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 {
Copy link
Member

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)
Copy link
Member

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
Copy link
Member

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
Copy link
Member

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"
Copy link
Member

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)
Copy link
Member

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" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ifの条件をそのままreturnしても良さそう

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants