我有以下結(jié)構(gòu)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}我想嵌入另一個(gè)結(jié)構(gòu),如:package routerimport ( "net/http" "townspeech/types" "townspeech/components/session" "townspeech/controllers/base")type authHandler struct { sidHandler handler authFuncHandler auth *session.Auth}以及使用 authHandler 結(jié)構(gòu)的函數(shù):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]如您所見,這兩個(gè)結(jié)構(gòu)體在同一個(gè)包中,字段部分不應(yīng)顯示為私有。我究竟做錯(cuò)了什么?
2 回答

動(dòng)漫人物
TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超10個(gè)贊
嵌入不適用于這樣的文字。
func registerAuthHandler(handler authFuncHandler, section string) http.Handler {
return &authHandler{
handler: handler,
sidHandler: sidHandler{section: section},
}
}

米脂
TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超3個(gè)贊
您不能在結(jié)構(gòu)文字中引用提升的字段。您必須創(chuàng)建嵌入類型,并通過類型名稱引用它。
&authHandler{
sidHandler: sidHandler{section: "bar"},
handler: "foo",
}
- 2 回答
- 0 關(guān)注
- 206 瀏覽
添加回答
舉報(bào)
0/150
提交
取消