1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【A Tour of Go】 Methods and interfaces/Exercise: Readers (22/26)

Posted at

A Tour of Go の Exercise を実施したときのメモ。

  • 最初 for i := 0; i < cap(b); i++ { と、実装したら
    panic: runtime error: index out of range
    になったので、for i := 0; i < len(b); i++ {に修正した。
  • len()、cap()の値は、それぞれ以下のようになった。 Array を分割してRead()を呼び出しているようだ。
    • len(b) は 1024
    • cap(b) は 2048
exercise-readers.go
package main

import "golang.org/x/tour/reader"

type MyReader struct{}

// TODO: Add a Read([]byte) (int, error) method to MyReader.
func (r MyReader) Read(b []byte) (int, error) {
	for i := 0; i < len(b); i++ {
		b[i] = 'A'
	}
	return len(b), nil
}

func main() {
	reader.Validate(MyReader{})
}
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?