dotelauv682684 2015-04-16 18:25
浏览 21
已采纳

为什么没有嵌入字段部分

I have the following struct

package router

import (
    "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
}

And I want to embed in another struct like:

package router

import (
    "net/http"
    "townspeech/types"
    "townspeech/components/session"
    "townspeech/controllers/base"
)

type authHandler struct {
    sidHandler
    handler authFuncHandler
    auth    *session.Auth
}

And the function, that use the authHandler struct:

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

The compiler complain:

# app/router
../../../router/funcs.go:9: unknown authHandler field 'section' in struct literal
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?

  • 写回答

2条回答 默认 最新

  • douxi3432 2015-04-16 18:40
    关注

    Embedding doesn't work with literals like that.

    func registerAuthHandler(handler authFuncHandler, section string) http.Handler {
        return &authHandler{
            handler: handler,
            sidHandler: sidHandler{section: section},
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 onlyoffice编辑完后立即下载,下载的不是最新编辑的文档
  • ¥15 求caverdock使用教程
  • ¥15 Coze智能助手搭建过程中的问题请教
  • ¥15 12864只亮屏 不显示汉字
  • ¥20 三极管1000倍放大电路
  • ¥15 vscode报错如何解决
  • ¥15 前端vue CryptoJS Aes CBC加密后端java解密
  • ¥15 python随机森林对两个excel表格读取,shap报错
  • ¥15 基于STM32心率血氧监测(OLED显示)相关代码运行成功后烧录成功OLED显示屏不显示的原因是什么
  • ¥100 X轴为分离变量(因子变量),如何控制X轴每个分类变量的长度。