खेल का मैदान लिंक: http://play.golang.org/p/Ebf5AuJlcPइंटरफेस में असंबंधित विधि की उपस्थिति पाठ/टेम्पलेट तोड़ती है?
type Foo interface {}
type Bar interface {
ThisIsABar()
}
// Person implements both Foo and Bar
type Person struct {
Name string
}
func (p Person) ThisIsABar() {}
type FooContext struct {
Something Foo
}
type BarContext struct {
Something Bar
}
func main() {
t := template.Must(template.New("test").Parse("{{ .Something.Name }}\n"))
// This works fine.
if err := t.Execute(os.Stdout, FooContext{Person{"Timmy"}}); err != nil {
fmt.Printf("Error: %s\n", err)
}
// With BarContext, containing the exact same Person but wrapped in a
// different interface, it errors out.
if err := t.Execute(os.Stdout, BarContext{Person{"Timmy"}}); err != nil {
fmt.Printf("Error: %s\n", err)
}
}
जब मैं (text/template
पैकेज के माध्यम से) एक टेम्पलेट {{ .Something.Name }}
युक्त प्रस्तुत करना, मैं इंटरफ़ेस Foo
जो कोई तरीकों में शामिल है के माध्यम से जा सकते हैं, और यह ठीक काम करता है। लेकिन अगर मैं इंटरफ़ेस Bar
के माध्यम से के बजाय जाओ, मैं मिलता है:
executing "test" at <.Something.Name>: can't evaluate field Name in type main.Bar
क्यों इंटरफेस पर एक असंबंधित विधि की उपस्थिति, कि यहां तक कि नहीं किया जाता है, टेम्पलेट का प्रतिपादन प्रभावित करता है?
संभावित डुप्लिकेट [टेम्पलेट उन क्षेत्रों का मूल्यांकन नहीं करेगा जो इंटरफ़ेस प्रकार अंतर्निहित प्रकार के रूप में हैं] (http://stackoverflow.com/questions/19554209/template-wont-evaluate-fields-that-are-interface-type -as-the-underlying-type) – nemo