Kadai2 shuheiktgw #33
Open
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.
io.Readerとio.Writerについて調べてみよう
標準パッケージでどのように使われているか
io.Reader
io.Readerは以下のように宣言されたインターフェイス読み込んだ内容を保存するためのbyte列
pを引数として受け取り, 実際に読み込んだbyte数nとエラーを返す.io.Writer
io.Writerは以下のように宣言されたインターフェイス書き込みたいbyte列
pを引数として受け取り,実際に書き込んだbyte数nとエラーを返す.io.Reader,io.Writerを満たす構造体の例io.Readerとio.Writerがあることでどういう利点があるのか具体例を挙げて考えてみる
OSはソケットや標準入出力など,本来的な意味でのファイルではないものを"ファイル"と呼び,open, write, read, closeといった抽象化されたインターフェイスを通じて,実装の詳細を隠蔽した状態で操作できるようになっている.Goではこのインターフェイスを言語レベルで踏襲することによって,言語の利用者に期待する挙動を伝えている.
インターフェイスの中で共通した振る舞いを一つの関数として提供することで冗長性を排除できる. 例えば
ioutil.ReadAllなどはその好例.ioutil.ReadAllはio.Readerを引数として受け取り, その中身を全て読みだすための関数だが, もしインターフェイスがなければ,os.FileのためのReadAllやbytes.BufferのためのReadAllを書かなくはならない.1回目の宿題のテストを作ってみて下さい