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)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay