On Wed, 2022-03-09 at 19:16 -0800, Nikhilesh Susarla wrote: > In https://go.dev/doc/effective_go#printing > I saw an example for printing our custom string output for the type. > The code below is from docs. > func (t *T) String() string { > return fmt.Sprintf("%d/%g/%q", t.a, t.b, t.c) > } > fmt.Printf("%v\n", t) > > > But rather the statement should be this right? fmt.Printf("%v\n", > t.String()) > Am I missing something? >
When the %v (or %s, %q and others[1]) verbs are used the `String() string` is used to construct the printed value. You can see the logic here[2]. [1] https://github.com/golang/go/blob/00535b839841227ba60c2de78fbf767088f865bc/src/fmt/print.go#L611 [2] https://github.com/golang/go/blob/00535b839841227ba60c2de78fbf767088f865bc/src/fmt/print.go#L623-L628 -- 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. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/6fabecec501d706ee876483cba1f5cefecf55df5.camel%40kortschak.io.