Why my Golang defined method not implemented implicitily while String() does












-2














In https://tour.golang.org/methods/11



It states Under the hood, interface values can be thought of as a tuple of a value and a concrete type



I define M as follows



script1



package main

import (
"fmt"
)

type I interface {
M() string
}
type T struct {
S string
w string
}
func (t T) M() string {
return "dddd"
}
func main() {
var i I
i = T{"Hello","eeee"}
fmt.Printf("(%v, %T)", i, i)
fmt.Println(i)
}


This prints out ({Hello eee}, main.T){Hello eee}
interface i has vaule {Hello eee} and type main.T



script2:



package main

import (
"fmt"
)

type I interface {
M() string
}
type T struct {
S string
w string
}
func (t T) M() string {
return "dddd"
}
func (t T) String() string {
return "ccccc"
}
func main() {
var i I
i = T{"Hello","eeee"}
fmt.Printf("(%v, %T)", i, i)
fmt.Println(i)
}


This prints out (ccccc, main.T)ccccc.



interface i has vaule ccccc and type main.T



Seems when i add String() as Stringer defined by the fmt package in script2. The String() is implemented implicitily,not sure why?



I thought in script2 i would have value "{Hello eee}" and type main.T










share|improve this question




















  • 2




    I don't understand the question, but fmt.Stringer is used by the fmt package for printing values. See also the Tour of Go: Stringers.
    – JimB
    Nov 21 at 0:18


















-2














In https://tour.golang.org/methods/11



It states Under the hood, interface values can be thought of as a tuple of a value and a concrete type



I define M as follows



script1



package main

import (
"fmt"
)

type I interface {
M() string
}
type T struct {
S string
w string
}
func (t T) M() string {
return "dddd"
}
func main() {
var i I
i = T{"Hello","eeee"}
fmt.Printf("(%v, %T)", i, i)
fmt.Println(i)
}


This prints out ({Hello eee}, main.T){Hello eee}
interface i has vaule {Hello eee} and type main.T



script2:



package main

import (
"fmt"
)

type I interface {
M() string
}
type T struct {
S string
w string
}
func (t T) M() string {
return "dddd"
}
func (t T) String() string {
return "ccccc"
}
func main() {
var i I
i = T{"Hello","eeee"}
fmt.Printf("(%v, %T)", i, i)
fmt.Println(i)
}


This prints out (ccccc, main.T)ccccc.



interface i has vaule ccccc and type main.T



Seems when i add String() as Stringer defined by the fmt package in script2. The String() is implemented implicitily,not sure why?



I thought in script2 i would have value "{Hello eee}" and type main.T










share|improve this question




















  • 2




    I don't understand the question, but fmt.Stringer is used by the fmt package for printing values. See also the Tour of Go: Stringers.
    – JimB
    Nov 21 at 0:18
















-2












-2








-2







In https://tour.golang.org/methods/11



It states Under the hood, interface values can be thought of as a tuple of a value and a concrete type



I define M as follows



script1



package main

import (
"fmt"
)

type I interface {
M() string
}
type T struct {
S string
w string
}
func (t T) M() string {
return "dddd"
}
func main() {
var i I
i = T{"Hello","eeee"}
fmt.Printf("(%v, %T)", i, i)
fmt.Println(i)
}


This prints out ({Hello eee}, main.T){Hello eee}
interface i has vaule {Hello eee} and type main.T



script2:



package main

import (
"fmt"
)

type I interface {
M() string
}
type T struct {
S string
w string
}
func (t T) M() string {
return "dddd"
}
func (t T) String() string {
return "ccccc"
}
func main() {
var i I
i = T{"Hello","eeee"}
fmt.Printf("(%v, %T)", i, i)
fmt.Println(i)
}


This prints out (ccccc, main.T)ccccc.



interface i has vaule ccccc and type main.T



Seems when i add String() as Stringer defined by the fmt package in script2. The String() is implemented implicitily,not sure why?



I thought in script2 i would have value "{Hello eee}" and type main.T










share|improve this question















In https://tour.golang.org/methods/11



It states Under the hood, interface values can be thought of as a tuple of a value and a concrete type



I define M as follows



script1



package main

import (
"fmt"
)

type I interface {
M() string
}
type T struct {
S string
w string
}
func (t T) M() string {
return "dddd"
}
func main() {
var i I
i = T{"Hello","eeee"}
fmt.Printf("(%v, %T)", i, i)
fmt.Println(i)
}


This prints out ({Hello eee}, main.T){Hello eee}
interface i has vaule {Hello eee} and type main.T



script2:



package main

import (
"fmt"
)

type I interface {
M() string
}
type T struct {
S string
w string
}
func (t T) M() string {
return "dddd"
}
func (t T) String() string {
return "ccccc"
}
func main() {
var i I
i = T{"Hello","eeee"}
fmt.Printf("(%v, %T)", i, i)
fmt.Println(i)
}


This prints out (ccccc, main.T)ccccc.



interface i has vaule ccccc and type main.T



Seems when i add String() as Stringer defined by the fmt package in script2. The String() is implemented implicitily,not sure why?



I thought in script2 i would have value "{Hello eee}" and type main.T







go






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 21:11

























asked Nov 21 at 0:02









Honord

12




12








  • 2




    I don't understand the question, but fmt.Stringer is used by the fmt package for printing values. See also the Tour of Go: Stringers.
    – JimB
    Nov 21 at 0:18
















  • 2




    I don't understand the question, but fmt.Stringer is used by the fmt package for printing values. See also the Tour of Go: Stringers.
    – JimB
    Nov 21 at 0:18










2




2




I don't understand the question, but fmt.Stringer is used by the fmt package for printing values. See also the Tour of Go: Stringers.
– JimB
Nov 21 at 0:18






I don't understand the question, but fmt.Stringer is used by the fmt package for printing values. See also the Tour of Go: Stringers.
– JimB
Nov 21 at 0:18














2 Answers
2






active

oldest

votes


















0














You should call fmt.Println(i.M()) ?

Why you want fmt call a function while its'n exist?



A Stringer is a type that can describe itself as a string. The fmt package (and many others) look for this interface to print values






share|improve this answer























  • Thanks for your answer. I understand i.M() will return dddd. I expect script1 return dddd like script2 return ccccc .
    – Honord
    Nov 21 at 1:37










  • @HenryXie why do you expect that?
    – zerkms
    Nov 21 at 2:47










  • The fmt package (and many others) look for this String() first to print values.
    – KibGzr
    Nov 21 at 3:46



















0














Refer: https://golang.org/pkg/fmt/#Stringer




Stringer is implemented by any value that has a String method, which
defines the “native” format for that value. The String method is used
to print values passed as an operand to any format that accepts a
string or to an unformatted printer such as Print.




In your case, in script1 you are just printing out the struct. In script2 you are providing what the builder should use when an unformatted print occurs which is a function that prints out "ccccc".






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403430%2fwhy-my-golang-defined-method-not-implemented-implicitily-while-string-does%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You should call fmt.Println(i.M()) ?

    Why you want fmt call a function while its'n exist?



    A Stringer is a type that can describe itself as a string. The fmt package (and many others) look for this interface to print values






    share|improve this answer























    • Thanks for your answer. I understand i.M() will return dddd. I expect script1 return dddd like script2 return ccccc .
      – Honord
      Nov 21 at 1:37










    • @HenryXie why do you expect that?
      – zerkms
      Nov 21 at 2:47










    • The fmt package (and many others) look for this String() first to print values.
      – KibGzr
      Nov 21 at 3:46
















    0














    You should call fmt.Println(i.M()) ?

    Why you want fmt call a function while its'n exist?



    A Stringer is a type that can describe itself as a string. The fmt package (and many others) look for this interface to print values






    share|improve this answer























    • Thanks for your answer. I understand i.M() will return dddd. I expect script1 return dddd like script2 return ccccc .
      – Honord
      Nov 21 at 1:37










    • @HenryXie why do you expect that?
      – zerkms
      Nov 21 at 2:47










    • The fmt package (and many others) look for this String() first to print values.
      – KibGzr
      Nov 21 at 3:46














    0












    0








    0






    You should call fmt.Println(i.M()) ?

    Why you want fmt call a function while its'n exist?



    A Stringer is a type that can describe itself as a string. The fmt package (and many others) look for this interface to print values






    share|improve this answer














    You should call fmt.Println(i.M()) ?

    Why you want fmt call a function while its'n exist?



    A Stringer is a type that can describe itself as a string. The fmt package (and many others) look for this interface to print values







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 21 at 1:40

























    answered Nov 21 at 1:34









    KibGzr

    1,466610




    1,466610












    • Thanks for your answer. I understand i.M() will return dddd. I expect script1 return dddd like script2 return ccccc .
      – Honord
      Nov 21 at 1:37










    • @HenryXie why do you expect that?
      – zerkms
      Nov 21 at 2:47










    • The fmt package (and many others) look for this String() first to print values.
      – KibGzr
      Nov 21 at 3:46


















    • Thanks for your answer. I understand i.M() will return dddd. I expect script1 return dddd like script2 return ccccc .
      – Honord
      Nov 21 at 1:37










    • @HenryXie why do you expect that?
      – zerkms
      Nov 21 at 2:47










    • The fmt package (and many others) look for this String() first to print values.
      – KibGzr
      Nov 21 at 3:46
















    Thanks for your answer. I understand i.M() will return dddd. I expect script1 return dddd like script2 return ccccc .
    – Honord
    Nov 21 at 1:37




    Thanks for your answer. I understand i.M() will return dddd. I expect script1 return dddd like script2 return ccccc .
    – Honord
    Nov 21 at 1:37












    @HenryXie why do you expect that?
    – zerkms
    Nov 21 at 2:47




    @HenryXie why do you expect that?
    – zerkms
    Nov 21 at 2:47












    The fmt package (and many others) look for this String() first to print values.
    – KibGzr
    Nov 21 at 3:46




    The fmt package (and many others) look for this String() first to print values.
    – KibGzr
    Nov 21 at 3:46













    0














    Refer: https://golang.org/pkg/fmt/#Stringer




    Stringer is implemented by any value that has a String method, which
    defines the “native” format for that value. The String method is used
    to print values passed as an operand to any format that accepts a
    string or to an unformatted printer such as Print.




    In your case, in script1 you are just printing out the struct. In script2 you are providing what the builder should use when an unformatted print occurs which is a function that prints out "ccccc".






    share|improve this answer


























      0














      Refer: https://golang.org/pkg/fmt/#Stringer




      Stringer is implemented by any value that has a String method, which
      defines the “native” format for that value. The String method is used
      to print values passed as an operand to any format that accepts a
      string or to an unformatted printer such as Print.




      In your case, in script1 you are just printing out the struct. In script2 you are providing what the builder should use when an unformatted print occurs which is a function that prints out "ccccc".






      share|improve this answer
























        0












        0








        0






        Refer: https://golang.org/pkg/fmt/#Stringer




        Stringer is implemented by any value that has a String method, which
        defines the “native” format for that value. The String method is used
        to print values passed as an operand to any format that accepts a
        string or to an unformatted printer such as Print.




        In your case, in script1 you are just printing out the struct. In script2 you are providing what the builder should use when an unformatted print occurs which is a function that prints out "ccccc".






        share|improve this answer












        Refer: https://golang.org/pkg/fmt/#Stringer




        Stringer is implemented by any value that has a String method, which
        defines the “native” format for that value. The String method is used
        to print values passed as an operand to any format that accepts a
        string or to an unformatted printer such as Print.




        In your case, in script1 you are just printing out the struct. In script2 you are providing what the builder should use when an unformatted print occurs which is a function that prints out "ccccc".







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 at 1:51









        Prajval M

        1,079317




        1,079317






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403430%2fwhy-my-golang-defined-method-not-implemented-implicitily-while-string-does%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            Refactoring coordinates for Minecraft Pi buildings written in Python