Golang noob question: Why can I not use the "%+v"
flag for a struct in the String() implementation method?
I have a struct where I want to implement a String()
method for pretty print. I like the answer given here, but I don't like to type, so I'm trying to modify it to return a string using the "%+v"
format flag for structs. from the fmt doc:
%v the value in a default format when printing structs, the plus flag (%+v) adds field names
This works fine if I simply call it with fmt.Printf("%+v", color)
, but if I try to put the +
flag in the String()
implementation, I get a stack overflow (my first chance to ask a "stack overflow" question on stackoverflow.com ;) )
I'm sure I'm not understanding a pointer reference here, or there is some recursion. I doubt this noob found my first Golang bug, so can someone please explain?
see go play demonstration here https://play.golang.org/p/13_qI8Iwwa
See Package fmt Docs:
Inside:
The call to
or
fmt.Println(c)
which callsfunc (c Color) String() string
again recursively causes overflow: try it on The Go PlaygroundAlso this works fine: https://play.golang.org/p/NYLtrxUeiA