Re: [go-nuts] Example of printing in go.dev

2022-03-09 Thread Kurtis Rader
On Wed, Mar 9, 2022 at 7:44 PM Nikhilesh Susarla 
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
> 
> .
>


-- 
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.


Re: [go-nuts] Example of printing in go.dev

2022-03-09 Thread Nikhilesh Susarla
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

Thank you
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.


Re: [go-nuts] Example of printing in go.dev

2022-03-09 Thread Kurtis Rader
On Wed, Mar 9, 2022 at 7:18 PM 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?
>

An object with a String() method causes that method to be used wherever a
string is desired. This includes the `%s` and `%v` formatting verbs. From
the paragraph preceding the example:

> If you want to control the default format for a custom type, all that's
required is to define a method with the signature String() string on the
type.

-- 
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%3DD9FeOzh9Dsv%2BMt%2BhMVfA%2BeQU_M_7zCNbMnsQDkLJT8EnQ%40mail.gmail.com.


Re: [go-nuts] Example of printing in go.dev

2022-03-09 Thread 'Dan Kortschak' via golang-nuts
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.


[go-nuts] Example of printing in go.dev

2022-03-09 Thread Nikhilesh Susarla
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?

Thank you
Nikhilesh

-- 
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/21a74232-88b2-4a46-9143-d6ffe27ddedfn%40googlegroups.com.