Common Standard Library Interfaces

io.Reader, io.Writer, fmt.Stringer

io.Reader, io.Writer, and fmt.Stringer are standard Go interfaces for reading data, writing data, and custom string formatting.

Use io.Reader to read data, io.Writer to write data, and fmt.Stringer to define custom string formatting for types. Implement these interfaces by defining the required methods on your type.

package main

import (
	"fmt"
	"io"
)

type MyData struct {
	value string
}

func (m MyData) Read(p []byte) (n int, err error) {
	return len(p), nil // Simplified for example
}

func (m MyData) Write(p []byte) (n int, err error) {
	return len(p), nil // Simplified for example
}

func (m MyData) String() string {
	return m.value
}

func main() {
	var r io.Reader = MyData{value: "test"}
	var w io.Writer = MyData{value: "test"}
	var s fmt.Stringer = MyData{value: "test"}
	fmt.Println(s)
	_ = r
	_ = w
}