Using golang html/template (same behavior with text/template ). If I have a structure with a member that has an interface type, I cannot access the elements of the base type (in particular, it tries to access the fields that are in the structure that implements the InnerInterface interface, but returns via the InnerInterface interface InnerInterface , not type of structure).
http://play.golang.org/p/ZH8wSK83oM
package main import "fmt" import "os" import "html/template" type InnerInterface interface{ InnerSomeMethod() } type MyInnerStruct struct { Title string } func (mis MyInnerStruct)InnerSomeMethod() { fmt.Println("Just to show we're satisfying the interface") } type MyOuterStruct struct { Inner InnerInterface } func main() { fmt.Println("Starting") arg := MyOuterStruct{Inner:MyInnerStruct{Title:"test1"}} err := template.Must(template.New("testtmpl").Parse("{{.Inner.Title}}")).Execute(os.Stdout, arg) if err != nil { panic(err) } }
Change: type MyOuterStruct struct { Inner InnerInterface } to a completely common interface, ie type MyOuterStruct struct { Inner interface{} } makes it correct. This makes me think that interface{} handled specifically by the rendering engine.
Is there a better way to do this than using interface{} whenever I want to dynamically evaluate such fields?
struct interface go field
Brad peabody
source share