Home > Backend Development > Golang > Why Doesn't fmt.Println Invoke My Stringer Interface's String Method When Passing a Value-Based Object?

Why Doesn't fmt.Println Invoke My Stringer Interface's String Method When Passing a Value-Based Object?

Susan Sarandon
Release: 2024-12-06 19:35:15
Original
478 people have browsed it

Why Doesn't fmt.Println Invoke My Stringer Interface's String Method When Passing a Value-Based Object?

Stringer Interface Confusion with Println: Understanding Value-Based vs. Pointer-Based Objects

Question:

In a scenario where an object implements the Stringer interface, why doesn't the object's String method get invoked when using fmt.Println if the object is value-based?

Code Example:

Consider the following Go code:

type Car struct {
    year int
    make string
}

func (c *Car) String() string {
    return fmt.Sprintf("{make:%s, year:%d}", c.make, c.year)
}

func main() {
    myCar := Car{year: 1996, make: "Toyota"}
    fmt.Println(myCar)
}
Copy after login

When myCar is a pointer, the String method gets invoked as expected. However, when myCar is a value, the default Go formatting is used instead.

Answer:

The reason for this behavior lies in the way Go interfaces work. When you specify a type that implements an interface (Stringer, in this case), Go expects that type to be the exact type of the interface. When you pass a value of type Car to fmt.Println, it is implicitly converted to interface{}, and there is no type Car in the interface{} type system. Instead, it's a type *Car (a pointer to Car).

The fmt.Println function uses a type switch to determine how to print the value based on its type. For a Stringer interface, it checks if the value implements the String method. Since Car (value-based) doesn't implement String, the default formatting is used. However, when you call myCar.String() explicitly, the compiler automatically converts it to (&myCar).String(), which has the correct *Car type and invokes the desired formatting method.

To ensure that the object is formatted as desired regardless of its type, you have two options:

  1. Implement String on Car (value-based): This requires creating a separate String method for Car that operates on value receivers, which would incur unnecessary object copying.
  2. Always pass a pointer to fmt.Println: By using fmt.Println(&myCar), you ensure that the passed value has the correct *Car type and the String method will be invoked as expected.

The above is the detailed content of Why Doesn't fmt.Println Invoke My Stringer Interface's String Method When Passing a Value-Based Object?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template