Home  >  Article  >  Backend Development  >  Why Does Go\'s `fmt.Println` Produce Unexpected Output When Embedding Types with Conflicting `String()` Methods?

Why Does Go\'s `fmt.Println` Produce Unexpected Output When Embedding Types with Conflicting `String()` Methods?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-24 09:08:10292browse

Why Does Go's `fmt.Println` Produce Unexpected Output When Embedding Types with Conflicting `String()` Methods?

Unexpected String() Method Behavior with Embedded Types

In Go, embedding allows types to include fields and methods from another type within their own structure. However, when multiple embedded types define a method with the same name (such as String()), certain scenarios can lead to unexpected behavior.

Consider the following code:

type Engineer struct {
    Person
    TaxPayer
    Specialization string
}

type Person struct {
    Name string
    Age  int
}

func (p Person) String() string {
    return fmt.Sprintf("name: %s, age: %d", p.Name, p.Age)
}

type TaxPayer struct {
    TaxBracket int
}

func (t TaxPayer) String() string {
    return fmt.Sprintf("%d", t.TaxBracket)
}

func main() {
    engineer := Engineer{
        Person: Person{
            Name: "John Doe",
            Age:  35,
        },
        TaxPayer: TaxPayer{3},
        Specialization: "Construction",
    }
    fmt.Println(engineer)
}

When this code is executed, it produces the following output:

{name: John Doe, age: 35 3 Construction}

This result may seem confusing, as there are multiple String() methods defined within the embedded types Person and TaxPayer. However, the ambiguity is resolved by the following rules:

  • If multiple embedded types define a String() method, it is considered an "illegal selector," meaning it cannot be accessed directly (by calling engineer.String(), for example).
  • As a result, no String() method is promoted to the embedding type (Engineer), and thus the default formatting (printing the field values) is used.
  • However, when the fmt package attempts to produce a default string representation for the Engineer value, it checks if the value implements the fmt.Stringer interface (which has a String() method).
  • Since the Person type implements fmt.Stringer, its String() method is called to produce the string representation of the fields (name and age).

Removing the Person.String() method or the TaxPayer.String() method resolves the ambiguity, allowing the remaining String() method to be used for the default formatting.

The key takeaway from this behavior is that embedded types promote their String() methods only if there is a single, unambiguous method defined. If multiple methods exist, the embedding type does not have a promoted String() method, and the default formatting is used.

The above is the detailed content of Why Does Go\'s `fmt.Println` Produce Unexpected Output When Embedding Types with Conflicting `String()` Methods?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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