On Wed, Mar 9, 2022 at 7:44 PM Nikhilesh Susarla <nikhilesh1...@gmail.com>
wrote:

> Okay. I get it.
> When I ran through go playground I missed that type receiver was of
> pointer type, so I was not sure why it didn't print as mentioned in the docs
>

It doesn't matter that the receiver "was of pointer type". That's just an
optimization to avoid copying a, possibly large, object before calling it's
String() method. Go ahead and modify the example to use a non-pointer
receiver. It should behave the same (it does for me).


> On Thursday, 10 March 2022 at 09:00:49 UTC+5:30 kortschak wrote:
>
>> 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/782337ca-4051-414a-a809-a6f88f5ffab1n%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/782337ca-4051-414a-a809-a6f88f5ffab1n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>


-- 
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

-- 
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/CABx2%3DD871uERNB9HoLHK_Ptig%3DzpP%3DwfZQh2pCJkMTSWZEpw-A%40mail.gmail.com.

Reply via email to