为什么字段部分没有嵌入?

huangapple go评论108阅读模式
英文:

Why the field section does not get embedded

问题

我有以下的结构体:

  1. package router
  2. import (
  3. "io"
  4. "net/http"
  5. "townspeech/components/i18n"
  6. "townspeech/components/policy"
  7. "townspeech/components/session"
  8. "townspeech/controllers/base"
  9. "townspeech/types"
  10. )
  11. type sidHandler struct {
  12. req *http.Request
  13. res http.ResponseWriter
  14. handler sidFuncHandler
  15. section string
  16. err *types.ErrorJSON
  17. sess *session.Sid
  18. }

我想将它嵌入到另一个结构体中,如下所示:

  1. package router
  2. import (
  3. "net/http"
  4. "townspeech/types"
  5. "townspeech/components/session"
  6. "townspeech/controllers/base"
  7. )
  8. type authHandler struct {
  9. sidHandler
  10. handler authFuncHandler
  11. auth *session.Auth
  12. }

并且使用authHandler结构体的函数:

  1. func registerAuthHandler(handler authFuncHandler, section string) http.Handler {
  2. return &authHandler{handler: handler, section: section}
  3. }

编译器报错:

  1. # app/router
  2. ../../../router/funcs.go:9: unknown authHandler field 'section' in struct literal
  3. FAIL app/test/account/validation [build failed]

如你所见,这两个结构体位于同一个包中,字段section不应该是私有的。我做错了什么?

英文:

I have the following struct

  1. package router
  2. import (
  3. "io"
  4. "net/http"
  5. "townspeech/components/i18n"
  6. "townspeech/components/policy"
  7. "townspeech/components/session"
  8. "townspeech/controllers/base"
  9. "townspeech/types"
  10. )
  11. type sidHandler struct {
  12. req *http.Request
  13. res http.ResponseWriter
  14. handler sidFuncHandler
  15. section string
  16. err *types.ErrorJSON
  17. sess *session.Sid
  18. }

And I want to embed in another struct like:

  1. package router
  2. import (
  3. "net/http"
  4. "townspeech/types"
  5. "townspeech/components/session"
  6. "townspeech/controllers/base"
  7. )
  8. type authHandler struct {
  9. sidHandler
  10. handler authFuncHandler
  11. auth *session.Auth
  12. }

And the function, that use the authHandler struct:

  1. func registerAuthHandler(handler authFuncHandler, section string) http.Handler {
  2. return &authHandler{handler: handler, section: section}
  3. }

The compiler complain:

  1. # app/router
  2. ../../../router/funcs.go:9: unknown authHandler field 'section' in struct literal
  3. FAIL app/test/account/validation [build failed]

As you can see, the two structs are in the same package, field section should not appear as private.
What am I doing wrong?

答案1

得分: 2

你不能在结构体字面值中引用提升的字段。你必须创建嵌入类型,并通过类型名称引用它。

  1. &authHandler{
  2. sidHandler: sidHandler{section: "bar"},
  3. handler: "foo",
  4. }
英文:

You can't refer to promoted fields in a struct literal. You have to create the embedded type, and refer to it by the type's name.

  1. &authHandler{
  2. sidHandler: sidHandler{section: "bar"},
  3. handler: "foo",
  4. }

答案2

得分: 2

嵌入式不适用于这样的文字。

  1. func registerAuthHandler(handler authFuncHandler, section string) http.Handler {
  2. return &authHandler{
  3. handler: handler,
  4. sidHandler: sidHandler{section: section},
  5. }
  6. }
英文:

Embedding doesn't work with literals like that.

  1. func registerAuthHandler(handler authFuncHandler, section string) http.Handler {
  2. return &authHandler{
  3. handler: handler,
  4. sidHandler: sidHandler{section: section},
  5. }
  6. }

huangapple
  • 本文由 发表于 2015年4月17日 02:25:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/29683047.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定