我有以下结构package routerimport ( "io" "net/http" "townspeech/components/i18n" "townspeech/components/policy" "townspeech/components/session" "townspeech/controllers/base" "townspeech/types")type sidHandler struct { req *http.Request res http.ResponseWriter handler sidFuncHandler section string err *types.ErrorJSON sess *session.Sid}我想嵌入另一个结构,如:package routerimport ( "net/http" "townspeech/types" "townspeech/components/session" "townspeech/controllers/base")type authHandler struct { sidHandler handler authFuncHandler auth *session.Auth}以及使用 authHandler 结构的函数:func registerAuthHandler(handler authFuncHandler, section string) http.Handler { return &authHandler{handler: handler, section: section}}编译器抱怨:# app/router../../../router/funcs.go:9: unknown authHandler field 'section' in struct literalFAIL app/test/account/validation [build failed]如您所见,这两个结构体在同一个包中,字段部分不应显示为私有。我究竟做错了什么?
2 回答
动漫人物
TA贡献1815条经验 获得超10个赞
嵌入不适用于这样的文字。
func registerAuthHandler(handler authFuncHandler, section string) http.Handler {
return &authHandler{
handler: handler,
sidHandler: sidHandler{section: section},
}
}
米脂
TA贡献1836条经验 获得超3个赞
您不能在结构文字中引用提升的字段。您必须创建嵌入类型,并通过类型名称引用它。
&authHandler{
sidHandler: sidHandler{section: "bar"},
handler: "foo",
}
- 2 回答
- 0 关注
- 186 浏览
添加回答
举报
0/150
提交
取消