Re: [go-nuts] fmt.Printf("%v") panics where %#v doesn't

2016-07-15 Thread 'Chris Manghane' via golang-nuts
In your example, MyError2 does not have an Error() method so it is not called. From the fmt package docs (golang.org/pkg/fmt): - 3. If the %v verb is used with the # flag (%#v) and the operand implements the GoStringer interface, that will be invoked. - If the format (which is implicitly

Re: [go-nuts] fmt.Printf("%v") panics where %#v doesn't

2016-07-15 Thread Thorsten von Eicken
Yup, doesn't quite make sense to me that Error() is called if it's an embedded struct and not when it's a struct field. See https://play.golang.org/p/Vp6Y-RETWZ On Friday, July 15, 2016 at 2:03:25 PM UTC-7, Thorsten von Eicken wrote: > > You're probably correct, see this: > > package main > > im

Re: [go-nuts] fmt.Printf("%v") panics where %#v doesn't

2016-07-15 Thread Matt Harden
This might make it clearer as well: https://play.golang.org/p/g3KdJUQRlK On Fri, Jul 15, 2016 at 2:03 PM Thorsten von Eicken wrote: > You're probably correct, see this: > > package main > > import ( > "fmt" > ) > > type MyError struct{ error } > type MyError2 struct { > foo int > err error > } >

Re: [go-nuts] fmt.Printf("%v") panics where %#v doesn't

2016-07-15 Thread Thorsten von Eicken
You're probably correct, see this: package main import ( "fmt" ) type MyError struct{ error } type MyError2 struct { foo int err error } func main() { err := MyError{} fmt.Printf("%%#v %#v\n", err) fmt.Printf("%%+v %+v\n", err) fmt.Printf("%%v %v\n", err) err2 := MyError2{} fmt.Printf("%%#v %#

Re: [go-nuts] fmt.Printf("%v") panics where %#v doesn't

2016-07-15 Thread Matt Harden
I think MyError has the Error method, which %v will use but %#v will not. Then it panics because it's trying to call error(nil).Error(). On Fri, Jul 15, 2016, 13:22 Thorsten von Eicken wrote: > I was surprised to see Printf %v panic on a struct that %#v prints fine. > Is this expected / intentio

[go-nuts] fmt.Printf("%v") panics where %#v doesn't

2016-07-15 Thread Thorsten von Eicken
I was surprised to see Printf %v panic on a struct that %#v prints fine. Is this expected / intentional / as designed? Test case: package main import ( "fmt" ) type MyError struct{ error } func main() { err := MyError{} fmt.Printf("%%#v %#v\n", err) fmt.Printf("%%+v %+v\n", err) fmt.Printf("%%