doujia9833 2013-10-23 23:30
浏览 25
已采纳

模板不会将接口类型的字段评估为基础类型

Using golang html/template (same behavior with text/template). If I have a struct with a member that is of an interface type, I cannot access members of the underlying type (specifically trying to access fields that are on a struct that implements interface InnerInterface but is return via the InnerInterface interface type, not the struct type).

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) }

}

Changing: type MyOuterStruct struct { Inner InnerInterface } to a totally generic interface, i.e. type MyOuterStruct struct { Inner interface{} } makes it render properly. This leads me to believe that interface{} is treated specially by the rendering engine.

Is there a better way to do this than to use interface{} whenever I want to be able to dynamically evaluate fields like this?

  • 写回答

1条回答 默认 最新

  • dtyqflrr775518 2013-10-24 01:24
    关注

    You're correct with saying that interface{} is handled differently by the rendering engine. Only interface{} values are unpacked, interface values that have a method set are not. I suppose the reasoning behind this is that if you have a interface type, you specifically limit the type to the method set. Therefore, you don't want the template engine trying to access members that may lie behind that interface.

    The 'problem' is caused by the function indirect in exec.go:

    func indirect(v reflect.Value) (rv reflect.Value, isNil bool) {
        for ; v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface; v = v.Elem() {
            if v.IsNil() {
                return v, true
            }
            if v.Kind() == reflect.Interface && v.NumMethod() > 0 {
                break
            }
        }
        return v, false
    }
    

    This method is called to get to the deepest value of a reflected value. Suppose you have a pointer on a pointer on a pointer, this function will get return the last of these. The same goes for interface values. The crux is that as soon as a interface value has more than 0 methods, the indirection stops there. Exactly the behaviour you're describing.

    As this seems to be intended behaviour, what you can do is to define a Title() string method in your interface and let it return the string.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分