Implement your own Reader!

Report a typo

In this assignment, we suggest that you implement the io.ReadWriter interface.

You already have a starting draft code; you just need to modify the Read() method so that it fills the p slice. Perhaps the built-in function copy will help you.

Write a program in Go
package main

import (
"fmt"
"io"
"log"
)

const Size = 1024

type WisdomBox struct {
quotes string
}

func (wb *WisdomBox) Write(p []byte) (n int, err error) {
wb.quotes += string(p)
return len(p), nil
}

func (wb *WisdomBox) Read(p []byte) (n int, err error) {
// read data to the p slice
return len(???), nil
}

// DO NOT delete or modify the code within the main() function!
func main() {
var box io.ReadWriter = &WisdomBox{}
fmt.Fprintln(box, "Never underestimate anyone")
fmt.Fprintln(box, "Home is where the heart is")
fmt.Fprintln(box, "You can be whoever you want to be")

p := make([]byte, Size)
n, err := box.Read(p)
if err != nil {
log.Fatal(err)
}
___

Create a free account to access the full topic