skiprope icon indicating copy to clipboard operation
skiprope copied to clipboard

Add `io.WriterTo` methods

Open chewxy opened this issue 8 years ago • 5 comments

There should be THREE io.WriterTo methods:

  • WriteTo(w io.Writer) (n int64, err error)
  • WriteToSubstr(w io.Writer, pointA, pointB int) error

The latter is essentially SubstrBytes but writing it to a io.Writer

chewxy avatar Oct 18 '17 12:10 chewxy

Can I get this one? -- wondering what's the reason to do that.

klebervirgilio avatar Oct 18 '17 14:10 klebervirgilio

Be my guest

chewxy avatar Oct 18 '17 20:10 chewxy

the reason you'd do that is because SubstrBytes creates and allocates a []byte. In applications where you'd have heavy writes to a string, you probably already have a []byte pool in stand by, already preallocated and ready to be used. Typically this comes in a bytes.Buffer. So might as well implement it as the io interfaces

chewxy avatar Oct 18 '17 21:10 chewxy

@chewxy Sorry, it isn't clear to me what WriteTo and WriteToSubstr should do. Would you mind to give me directions?

klebervirgilio avatar Oct 25 '17 15:10 klebervirgilio

WriteTo is like fmt.Fprintf. It writes from char 0 to char n to io.Writer that is passed in. WriteToSubstr is like WriteTo but instead of writing from 0 to n , the user passes in the range

chewxy avatar Oct 25 '17 19:10 chewxy