dongli9894 2018-02-15 17:52
浏览 53

导入供应商的软件包时输入错误

I am currently trying to export all shared components from multiple services to one new package and then reimporting this package to the services.

I quickly came accross the following problem: In my utility package I create a function:

package utils

import "github.com/rs/zerolog"
func CreateLogger(logLevel string, w io.Writer) Logger {
    ...
    return zerolog.New(w).With().Timestamp().Logger()
}

So now this package references "github.com/rs/zerolog"

Now in my services I want to use this method like this:

import "github.com/rs/zerolog"
var (
    logger      zerolog.Logger
)

func New() {
    logger = utils.CreateLogger("info", os.Stdout)
}

Now this also references "github.com/rs/zerolog", but throws the error:

cannot use utils.CreateLogger("info", os.Stdout) (type "MY_REPO_PATH/vendor/VENDORED_REPO_PATH/vendor/github.com/rs/zerolog".Logger) as type "MY_REPO_PATH/vendor/github.com/rs/zerolog".Logger in assignment

So I understand why this is happening and I found a solution by wrapping the zerologger in my utils into a struct like this:

type Logger struct {
    Logger zerolog.Logger
}

Now this works, but it is inconvenient because I have to call logger.Logger.Info()... instead of logger.Info() in my code.

It looks to me like this is not the perfect solution to this problem and I would like to know if there is a better way to solve it.

Best Regards, Jonathan

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 微信会员卡接入微信支付商户号收款
    • ¥15 如何获取烟草零售终端数据
    • ¥15 数学建模招标中位数问题
    • ¥15 phython路径名过长报错 不知道什么问题
    • ¥15 深度学习中模型转换该怎么实现
    • ¥15 HLs设计手写数字识别程序编译通不过
    • ¥15 Stata外部命令安装问题求帮助!
    • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
    • ¥15 TYPCE母转母,插入认方向
    • ¥15 如何用python向钉钉机器人发送可以放大的图片?