I want to simulate bytes.ErrTooLarge panic error on bytes.Buffer.Write method and test panic handling. I have tried to write unlimited data amount to exceed memory but then whole test crashed. What are the other options?
Sounds like a job for a mock object. Use this (badBuffer) in place of your bytes.Buffer during your test.
type badBuffer bytes.Buffer
func (b *badBuffer) Write(p []byte) (n int, err error) {
panic(bytes.ErrTooLarge)
}