Adding a new method to an existing (standard) type

Something along this approach (caution: untested code):

type reader struct{
        *bufio.Reader // 'reader' inherits all bufio.Reader methods
}

func newReader(rd io.Reader) reader {
        return reader{bufio.NewReader(rd)}
}

// Override bufio.Reader.ReadBytes
func (r reader) ReadBytes(delim byte) (line []byte, err error) {
        // here goes the monkey patch
}

// Or

// Add a new method to bufio.Reader
func (r reader) ReadBytesEx(delims []byte) (line []byte, err error) {
        // here goes the new code
}

EDIT: I should have noted that this doesn't help to access the original package internals (non exported entities). Thanks Abhay for pointing that out in your comment.

Tags:

Go