FAQ
String() GotCha please explain to a tired old mind

The linked code below runs as is but
reverse comment the type, return and assignment statements
and this program runs out of memory and produces the error:

throw: out of memory (FixAlloc)



http://play.golang.org/p/7GiWcDgqHe

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Peter at Feb 2, 2013 at 12:54 pm
    In order to print an interface using the "%s" format, the Printf function
    calls the .String() method. Which is what you're printing from, leading to
    an infinite recursion.

    Typically, a String() method returns the string, it does not call
    fmt.Printf directly.
    On Saturday, 2 February 2013 12:42:36 UTC, YardVox wrote:

    String() GotCha please explain to a tired old mind

    The linked code below runs as is but
    reverse comment the type, return and assignment statements
    and this program runs out of memory and produces the error:

    throw: out of memory (FixAlloc)



    http://play.golang.org/p/7GiWcDgqHe
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • YardVox at Feb 2, 2013 at 2:14 pm
    I isolated my problem and fixed it but need to better understand the fix.

    See new linked code below
    http://play.golang.org/p/Ygm6uOfdVk

    The fix is to cast in inbound Force type to a string so that Sprintf see it
    as a value not a type via: string(in).
    When a type is passed to Sprintf should the compiler catch this or not?
    At my current level of knowledge I do no understand
    why type Force string when passed to (in Force) String() does not tell
    it ... I am a string. It must
    be saying I am a type Force (and I (human) must cast it as a string).. Is
    this type Force some kinda interface{} behind the
    scenes that I do not yet fully comprehend?


    On Saturday, February 2, 2013 7:42:36 AM UTC-5, YardVox wrote:

    String() GotCha please explain to a tired old mind

    The linked code below runs as is but
    reverse comment the type, return and assignment statements
    and this program runs out of memory and produces the error:

    throw: out of memory (FixAlloc)



    http://play.golang.org/p/7GiWcDgqHe
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Ian Lance Taylor at Feb 2, 2013 at 2:51 pm

    On Sat, Feb 2, 2013 at 6:14 AM, YardVox wrote:
    I isolated my problem and fixed it but need to better understand the fix.

    See new linked code below
    http://play.golang.org/p/Ygm6uOfdVk

    The fix is to cast in inbound Force type to a string so that Sprintf see it
    as a value not a type via: string(in).
    When a type is passed to Sprintf should the compiler catch this or not?
    At my current level of knowledge I do no understand
    why type Force string when passed to (in Force) String() does not tell it
    ... I am a string. It must
    be saying I am a type Force (and I (human) must cast it as a string).. Is
    this type Force some kinda interface{} behind the
    scenes that I do not yet fully comprehend?
    The type of a value of type Force is, well, Force. When you pass it
    to fmt.Sprintf it will be converted to a value of type interface{},
    which will record that its dynamic type is Force and the value is
    whatever the value is. When fmt.Sprintf prints that value, it will
    check if Force has a String method, and call it if it does, leading to
    infinite recursion in your first example.

    When you add the conversion to string before calling fmt.Sprintf,
    fmt.Sprintf will receive an interface{} with a dynamic type of string,
    not Force. Thus it will act differently.

    Ian

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • YardVox at Feb 2, 2013 at 10:22 pm
    Thank you Peter and Ian for taking time from your weekend to explain the
    workings of Go

    Postmortem lessons learned:
    1) Go is not C ... like C .. but not C
    2) If you import fmt then "Closely read about the functions you call". In
    this case Sprintf and Println.
    The Package Doc for fmt.Println is:
    func Println(a ...interface{}) (n int, err error)

    There it is. a...interface{}. This is not a static type.
    type Force string // is a static type (least a non-interface-type) but
    when you pass it to fmt.Println it becomes dynamic
    and seeing the .String() recursion ... it is interface{} all the way down.

    So when a Go func has a...interface{} as the argument ... Type processing
    occurs when a Type is passed to the function.
    In this case import fmt got me the Stringer interface and
    when I defined a String() func for type Force I told Go that every time
    the Stringer interface is caledl handle zforce.String() if called.




    On Saturday, February 2, 2013 7:42:36 AM UTC-5, YardVox wrote:

    String() GotCha please explain to a tired old mind

    The linked code below runs as is but
    reverse comment the type, return and assignment statements
    and this program runs out of memory and produces the error:

    throw: out of memory (FixAlloc)



    http://play.golang.org/p/7GiWcDgqHe
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Nate Finch at Feb 2, 2013 at 11:29 pm
    Really, it's just that fmt.Sprint calls the type's .String() method if it
    has one, so you better make sure the String method doesn't call fmt.sprint

    Many of us already hit this early in our Go careers and now account for it
    automatically.
    On Saturday, February 2, 2013 5:22:26 PM UTC-5, YardVox wrote:

    Thank you Peter and Ian for taking time from your weekend to explain the
    workings of Go

    Postmortem lessons learned:
    1) Go is not C ... like C .. but not C
    2) If you import fmt then "Closely read about the functions you call". In
    this case Sprintf and Println.
    The Package Doc for fmt.Println is:
    func Println(a ...interface{}) (n int, err error)

    There it is. a...interface{}. This is not a static type.
    type Force string // is a static type (least a non-interface-type) but
    when you pass it to fmt.Println it becomes dynamic
    and seeing the .String() recursion ... it is interface{} all the way down.

    So when a Go func has a...interface{} as the argument ... Type processing
    occurs when a Type is passed to the function.
    In this case import fmt got me the Stringer interface and
    when I defined a String() func for type Force I told Go that every time
    the Stringer interface is caledl handle zforce.String() if called.




    On Saturday, February 2, 2013 7:42:36 AM UTC-5, YardVox wrote:

    String() GotCha please explain to a tired old mind

    The linked code below runs as is but
    reverse comment the type, return and assignment statements
    and this program runs out of memory and produces the error:

    throw: out of memory (FixAlloc)



    http://play.golang.org/p/7GiWcDgqHe
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • YardVox at Feb 2, 2013 at 11:56 pm
    Nate, Take another look inside of the .String() function
    ( I am not saying Sprintf inside .String() is proper style but it is not
    the problem)
    Note that when Sprintf see the argument as a value/static (ie string(in) or
    in.force or reflect.ValueOf(in) .... ) it breaks the recursion of .String()
    and gets to EndOfMain
    when Sprintf sees the argument as type Force it calls zforce.String()
    which calls Sprintf(type Force) which calls...............zforce.String()
    ...... infinite recursion
    On Saturday, February 2, 2013 7:42:36 AM UTC-5, YardVox wrote:

    String() GotCha please explain to a tired old mind

    The linked code below runs as is but
    reverse comment the type, return and assignment statements
    and this program runs out of memory and produces the error:

    throw: out of memory (FixAlloc)



    http://play.golang.org/p/7GiWcDgqHe
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Nate Finch at Feb 3, 2013 at 1:15 am
    I think we're saying the same thing :)

    On Saturday, February 2, 2013 6:56:42 PM UTC-5, YardVox wrote:

    Nate, Take another look inside of the .String() function
    ( I am not saying Sprintf inside .String() is proper style but it is not
    the problem)
    Note that when Sprintf see the argument as a value/static (ie string(in)
    or in.force or reflect.ValueOf(in) .... ) it breaks the recursion of
    .String() and gets to EndOfMain
    when Sprintf sees the argument as type Force it calls zforce.String()
    which calls Sprintf(type Force) which calls...............zforce.String()
    ...... infinite recursion
    On Saturday, February 2, 2013 7:42:36 AM UTC-5, YardVox wrote:

    String() GotCha please explain to a tired old mind

    The linked code below runs as is but
    reverse comment the type, return and assignment statements
    and this program runs out of memory and produces the error:

    throw: out of memory (FixAlloc)



    http://play.golang.org/p/7GiWcDgqHe
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedFeb 2, '13 at 12:42p
activeFeb 3, '13 at 1:15a
posts8
users4
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase