DEV Community

Nitin Bansal
Nitin Bansal

Posted on

A very very innocent☺️ infinite recursion trap🕸.. must read😱

#go

Here it is... try to figure out the why part before reading the explanation later...

type TT float64

func (t TT) String() string {
    return fmt.Sprintf("%v", t)
}

func main() {
    var t TT = 12.34
    t.String()
}
Enter fullscreen mode Exit fullscreen mode

Explanation:

Print methods that do not use any format specifiers internally call String() method on respective data. Example: Println or Printf with %v.

Notice that %v too behaves similarly..

Top comments (0)