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