I have a struct that contains a pointer to another struct. That is,
type InnerStruct struct {
whatever bool
}
type OuterStruct struct {
is *InnerStruct
}
If I print out an instance of OuterStruct
with %+v
, then the inner struct is not expanded like it would be if it weren't a pointer. I understand and accept why that is, but because I don't have any chance of recursion, I'd like to get the full thing printed out.
It occurred to me that I might be able to implement the Formatter
interface on a pointer receiver of InnerStruct
in order to get the behavior I want, and indeed, the following does exactly what I want, at least for the cases I had (perhaps there are ways in which it could go horribly wrong; please tell me!):
func (is *InnerStruct) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
if s.Flag('+') {
fmt.Fprintf(s, "&%+v", *is)
} else if s.Flag('#') {
fmt.Fprintf(s, "&%#v", *is)
} else {
fmt.Fprintf(s, "&%v", *is)
}
case 's':
fmt.Fprintf(s, "&%s", *is)
}
}
But when I run go vet
on the code, it complains:
./thing.go:341: Fprintf format %s has arg *s of wrong type thing.InnerStruct
Is there a better way to get at the default string representation of a struct that avoids this issue with vet
?
FWIW, I tried go-spew
, and got (almost) the exact output I wanted, so I'll probably just go down that route, but I'd still like to know what, if anything, could be done to satisfy vet
.