I was writing a program that reads data from a io.Reader and caches them in a bytes.Buffer.
type SecureReader struct {
pipe io.Reader
shared *[32]byte
decrypted bytes.Buffer
}
func (s SecureReader) Read(b []byte) (int, error) {
s.decryptPipeIntoBuffer()
return s.decrypted.Read(b)
}
func (s SecureReader) decryptPipeIntoBuffer() (int, error) {/*Lots of code...*/}
I first used a value receiver, because I thought they were the same. However, I noticed my method does not do anything when invoked: SecureReader.Read() would always return io.EOF.
I banged my head around and changed the receiver type to
func (s *SecureReader) decryptPipeIntoBuffer() (int, error) {/*Lots of code...*/}
Now my code magically works. What is going on?