You have implemented error on *Thing, so it is *that* which fmt is running, 
which then in turn runs the implementation from your wrapped error.

If you don't wish fmt to treat your *Thing as an error, you must remove 
method Error() string from it.



On Tuesday, August 28, 2018 at 5:15:31 PM UTC-7, Louki Sumirniy wrote:
>
> I discovered quite by accident and now I can't find anything saying as 
> such, but this example
>
> package main
>
> import (
>   "fmt"
>   "errors"
> )
>
> type Thing struct {
>   err error
> }
>
> type thing interface {
>   Error() string
> }
>
> func (t *Thing) Error() string {
>   return t.err.Error()
> }
>
> func main() {
>   t := new(Thing)
>   t.err = errors.New("testing")
>   fmt.Println(t)
> }
>
> https://play.golang.org/p/xBIGIvSZkqO
>
> as you can see by running it, prints the error value inside the struct. 
>
> I am writing a library where I am using a 'pipeline' model so I can string 
> pointer methods together in a chain, which requires putting an error value 
> inside the structure, and then it does this when I print the struct. It's 
> quite handy but unexpected. I assume if a struct satisfies the error 
> interface it calls it to generate the string.
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to