generated from gopherdojo/template
-
Notifications
You must be signed in to change notification settings - Fork 0
kynefuk / 課題2 #18
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
Open
kynefuk / 課題2 #18
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
<課題 2> io.Reader と io.Writer
io.Reader とは
一連のデータ(バイト列)の読み込み処理を抽象化したインターフェース。
読み込まれる対象データとしてバイト型の配列を受け取り、読み込んだバイト数とエラーを戻り値とする。
io.Writer とは
一連のデータ(バイト列)の書き込み処理を抽象化したインターフェース
書き込まれる対象データとしてバイト型の配列を受け取り、書き込んだバイト数とエラーを戻り値とする。
標準パッケージでどのように使われているか
データの入出力を表現する様々なパッケージで io.Reader や io.Writer が実装している。
(標準入力を表す
os.Stdin、標準出力を表すos.Stdout、ファイルの入出力を表現するos.Fileや、通信の入出力を表現するnet.Connなど)io.Reader と io.Writer があることでどういう利点があるのか具体例を挙げて考えてみる
入力処理と出力処理の仕様をインターフェースとして定義しておくことで、様々な入出力処理を統一的に扱うことができ、プログラムの柔軟性が高くなる。
例えば、何らかの読み込み処理を行う関数の引数の型を
io.Readerとすることで、io.Readerを実装している型であれば何でも受けることができる。コマンドラインのの処理内容を出力する標準出力や、エラーを表示する標準エラー出力のフィールドの型として io.Writer を指定することで、テスト時に、
bytes.Bufferなどに切り替えることができ、テスト容易性を高めることができる。1回目の課題のテストを作ってみて下さい
テストのしやすさを考えてリファクタリングしてみる
テストしやすいように関数の引数を構造体ではなく、stringやintのようなプリミティブ型を受け取るように修正。
テストのカバレッジを取ってみる
テーブル駆動テストを行う
一部のテストをテーブル駆動で書きました。
テストヘルパーを作ってみる