0

Normally, I can print all properties of an object with:

c.Infof("car: %+v", car)

But one struct has a String() method. I think that this causes the line above to only print what the String() method returns.

How can I override this and force print all properties of that struct?

JimB
  • 104,193
  • 13
  • 262
  • 255
user1283776
  • 19,640
  • 49
  • 136
  • 276

1 Answers1

6

An easy workaround is to use the %#v verb:

package main

import (
    "fmt"
)

type someStruct struct {
    a int
    b int
}

func (someStruct) String() string {
    return "this is the end"
}

func main() {
    fmt.Printf("%+v\n", someStruct{1, 2})
    fmt.Printf("%#v\n", someStruct{1, 2})
}

This prints:

this is the end
main.someStruct{a:1, b:2}
cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • 2
    @RickyA: it helps to read through the [`fmt` documentation:](https://golang.org/pkg/fmt/) `%#v a Go-syntax representation of the value`. (note that `%#v` is also modifiable via the GoStringer interface) – JimB May 31 '16 at 15:41