s/fmt.String/fmt.Sprintf/

On Thursday, September 1, 2016 at 12:25:24 PM UTC+3, dja...@gmail.com wrote:
>
> Hi,
> Do you know what infinite recursion is ?
> fmt.String use your String method
>
> see https://play.golang.org/p/jeOilZW7JU
>
>
> On Thursday, September 1, 2016 at 11:59:12 AM UTC+3, Muhammad Shulhan 
> wrote:
>>
>> Dear all,
>>
>> I will describe the problem step by step, please bear with me.
>>
>> Lets take a look at first example ,
>>
>> package main
>>>
>>> import (
>>>  "fmt"
>>> )
>>>
>>> type Slices []string
>>>
>>> func (sl *Slices) String() string {
>>>  // OK
>>>  return fmt.Sprint(sl)
>>> }
>>>
>>> func main() {
>>>  var slices Slices
>>>
>>>  fmt.Print("slices:", slices)
>>> }
>>>
>>
>>
>> Run: https://play.golang.org/p/Cjb0xCVaCC
>> Output:
>>
>> slices:[]
>>>
>>
>> Let change String() return to fmt.Sprint(*sl).
>>
>> Run: https://play.golang.org/p/St6TyltA56
>> Output:
>>
>> slices:[]
>>>
>>
>> Using the above prototype lets build a flag that parse Slices. Here is the 
>> code that return without stack overflow,
>>
>> package main
>>>
>>> import (
>>> "flag"
>>> "fmt"
>>> )
>>>
>>> type Slices []string
>>>
>>> func (sl *Slices) String() string {
>>> return fmt.Sprint(*sl)
>>> }
>>>
>>> func (sl *Slices) Set(vv string) error {
>>> // STUB for clarity
>>> return nil
>>> }
>>>
>>> func main() {
>>> var sl Slices
>>> flag.Var(&sl, "slices", "Test parsing slice flag.")
>>> flag.Parse()
>>>
>>> fmt.Printf("slices: %s\n", sl)
>>> }
>>
>>
>> Run: https://play.golang.org/p/Sol4C8hRk7
>> Output:
>>
>> slices: []
>>>
>>
>> Last one, lets change the String return to "fmt.Sprint(sl)".
>>
>> Run: https://play.golang.org/p/550Krvim77
>> Output:
>>
>> runtime: goroutine stack exceeds 250000000-byte limit 
>>>
>> fatal error: stack overflow
>>>
>>  
>> Question: is this a bug? If it is I would report one.
>>
>> PS:
>>
>> Previous discussion that I found mentioning the same problem:
>>
>> * 
>> https://groups.google.com/forum/#!searchin/golang-nuts/fmt.Sprint|sort:relevance/golang-nuts/jJ72z0Ssahc/FDwArwNCiw4J
>>
>

-- 
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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to