davecgh / go-spew

Implements a deep pretty printer for Go data structures to aid in debugging
ISC License
5.98k stars 361 forks source link

spew.dump stack overflow problem #76

Open nkbai opened 6 years ago

nkbai commented 6 years ago
package main

import "github.com/davecgh/go-spew/spew"

type A struct {
    AMember int
}

func (this *A) String() string {
    return spew.Sdump(this)
}

type B struct {
    BMember A
}

func main() {
    spew.Config.DisableMethods = false
    b := &B{A{32}}
    spew.Dump(b)
}
nkbai commented 6 years ago

I hope that fmt.sprintf and spew.dump work together in my project how to ? thanks

dmitshur commented 6 years ago

When you execute spew.Sdump(this) in the A.String method, spew runs, sees that the type A implements the fmt.Stringer interface, and calls String, which in turn calls spew again. So it recurses forever.

You shouldn’t call spew in a String method.

nkbai commented 6 years ago

thanks ,i have removed spew.sdump from stringer method