String() which changes the ouput of Println

I am studying Golang from a course on udemy taught by Todd Mcleod

Todd used a code from godoc.org/sort which is under example link (https://godoc.org/sort#ex-package) of the sort package page.

I can’t understand why the String() function changes the ouput given by Println, without calling it.

From the documentation:

If the format (which is implicitly %v for Println etc.) is valid for a string (%s %q %v %x %X), the following two rules apply:

  1. If an operand implements the error interface, the Error method will be invoked to convert the object to a string, which will then be formatted as required by the verb (if any).

  2. If an operand implements method String() string, that method will be invoked to convert the object to a string, which will then be formatted as required by the verb (if any).

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.