# Default String() function - question

**URL:** <https://forum.golangbridge.org/t/default-string-function-question/20457>\
**Category:** Getting Help\
**Created:** [September 4, 2020, 5:03am UTC](https://forum.golangbridge.org/t/default-string-function-question/20457 "2020-09-04T05:03:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![JOhn\_Stuart](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/john_stuart/32/5444_2.png) [@JOhn\_Stuart](https://forum.golangbridge.org/u/JOhn_Stuart)\
**Post date:** [September 4, 2020, 5:03am UTC](https://forum.golangbridge.org/t/default-string-function-question/20457/1 "2020-09-04T05:03:11Z")

</div>

I define a struct, lets call it S. Just by doing that, I have created an infinite data types: S, pointer to S, pointer to pointer to S, etc

Out of this infinite serie of data types, only one of them has a default String() function already implemented, and that is S. \<- is this true ? (1)

Because S already has a default String() method, if I define a String() method for \*S (pointer to S type), this will have the disadvantage that it works when called for a \*S but it will not get called for S (without pointer). When calling it for S (regular type, no pointer) the default String() method still gets invoked. \<- is this true ?

That is why, when creating a custom String() function it is beneficial that its receiver is type S not pointer to S. Because \*S has no default String() function, when calling String() on a pointer receiver, my custom String() (defined for S without pointer) is invoked. \<- is this true ?

For the type \*\*S (pointer of pointer of S), my custom String() function written for S, does not get involved. \<- why is this, it contradicts statement (1)

Help me figure out the truth, hopefully this truth will set me free of my doubts 🙂  
also look at t his example [https://play.golang.org/p/tQ\_nfRHk8w4](https://play.golang.org/p/tQ_nfRHk8w4)

---

<div class="post-metadata">

**Author:** ![skillian](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/skillian/32/7876_2.png) [@skillian](https://forum.golangbridge.org/u/skillian)\
**Post date:** [September 5, 2020, 1:10pm UTC](https://forum.golangbridge.org/t/default-string-function-question/20457/2 "2020-09-05T13:10:24Z")

</div>

> [@JOhn\_Stuart](#):
>
> Out of this infinite serie of data types, only one of them has a default String() function already implemented, and that is S. ← is this true ? (1)

No. `String` functions are not automatically implemented. The “print family” of functions have default handling of builtin types, and then type assertion checks to see if the type implements `fmt.Formatter`, `fmt.GoStringer` or `fmt.Stringer`, and ultimately falls back to building a representation with the `reflect` package if the type doesn’t define one.

> [@JOhn\_Stuart](#):
>
> Because S already has a default String() method, if I define a String() method for \*S (pointer to S type), this will have the disadvantage that it works when called for a \*S but it will not get called for S (without pointer). When calling it for S (regular type, no pointer) the default String() method still gets invoked. ← is this true ?

I’m not sure how to answer this. You cannot define a `String` method on both a `T` and a `*T`: [Go Playground - The Go Programming Language](https://play.golang.org/p/mfT2bgSQ5dm).

You can call a `(*T).String` function on a variable or variable of type `T`; the compiler implicitly takes the address of the variable (or struct field, array/slice element, etc.) and uses that: [Go Playground - The Go Programming Language](https://play.golang.org/p/U1FtwgG1hfv) However that doesn’t happen if the value is boxed into an interface, such as when the value is passed into any of the “print family” of functions.

> [@JOhn\_Stuart](#):
>
> That is why, when creating a custom String() function it is beneficial that its receiver is type S not pointer to S. Because \*S has no default String() function, when calling String() on a pointer receiver, my custom String() (defined for S without pointer) is invoked. ← is this true ?

Beneficial is a really subjective term, but I agree with you here 🙂

> [@JOhn\_Stuart](#):
>
> For the type \*\*S (pointer of pointer of S), my custom String() function written for S, does not get involved. ← why is this, it contradicts statement (1)

I don’t know why this is, but Go doesn’t allow methods to be defined on double-pointers, even if the pointers are defined types: [Go Playground - The Go Programming Language](https://play.golang.org/p/Toe7_F3_PK_P), so there’s no implicit call to `T.String` from a `(**T).String`.

---

<div class="post-metadata">

**Author:** ![system](https://forum.golangbridge.org/uploads/default/original/2X/5/5ad234f7c3dbc68b6cc72d338ce4f2973d9fd81c.png) [@system](https://forum.golangbridge.org/u/system)\
**Post date:** [December 4, 2020, 1:10pm UTC](https://forum.golangbridge.org/t/default-string-function-question/20457/3 "2020-12-04T13:10:27Z")

</div>

Closed on December 4, 2020, 1:10pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
