在golang中类型不一致,无法将用作

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

Inconsistent type in golang, cannot use <Type> as <Type>

问题

我正在使用Go编写一个应用程序,其中使用了Logger对象类型。
在其中,我使用了另一个应用程序,该应用程序也使用相同的Logger对象类型:
App1:

import "gitlab.sio.com/go/zlog"

var logger = zlog.New(append(opts,
	zlog.App(c.Name, typ, version),
	zlog.Env(c.Environment),
)...)
....
router.GET("/get", GetHandler(logger))
....
func GetHandler(logger *zlog.Logger){
    ....
    mdl, _ := security.New(*logger)
    ....
}

App2(security.New来自security库):

package security

import "gitlab.sio.com/go/zlog"

Middleware struct {
	log             zlog.Logger
}

func New(log zlog.Logger){
...
mdw := Middleware{}
mdw.log = log
}

我在以下代码行中遇到的错误是:

mdl, _ := security.New(*logger)

错误信息为:

无法将*logger(类型为"gitlab.sio.com/go/furtif/vendor/gitlab.sio.com/go/zlog"的Logger)作为参数传递给security.New中类型为"gitlab.sio.com/go/security/vendor/gitlab.sio.com/go/zlog"的Logger。

英文:

I am writing an application in Go that uses a Logger object type.
<br>In it I use another application that uses the same logger object type:
<BR>App1:

import &quot;gitlab.sio.com/go/zlog&quot;

var logger = zlog.New(append(opts,
	zlog.App(c.Name, typ, version),
	zlog.Env(c.Environment),
)...)
....
router.GET(&quot;/get&quot;, GetHandler(logger))
....
func GetHandler(logger *zlog.Logger){
    ....
    mdl, _ := security.New(*logger)
    ....
}

App2(security.New from security lib):

package security

import &quot;gitlab.sio.com/go/zlog&quot;

Middleware struct {
	log             zlog.Logger
}

func New(log zlog.Logger){
...
mdw := Middleware{}
mdw.log = log
}

The error what I'm getting at line

> mdl, _ := security.New(*logger)

is:

> cannot use *logger (type "gitlab.sio.com/go/furtif/vendor/gitlab.sio.com/go/zlog".Logger) as type "gitlab.sio.com/go/security/vendor/gitlab.sio.com/go/zlog".Logger in argument to security.New

答案1

得分: 3

问题是由于在两个不同的供应商文件夹中导入了相同的库。如果你只想将应用程序2用作库,那么删除应用程序2中的供应商文件夹将解决这个问题。

英文:

The issue is due to the same library imported in two different vendor folders.
If you are trying to use the application 2 only as a library, removing the vendor folder in the application 2 will solve this issue.

答案2

得分: 1

你需要修复你的导入语句,以正确指向导入类型所属的位置。请阅读错误信息。

英文:

You need to fix your import statements to properly point the import types from where they should belong to. Read the error message.

答案3

得分: -1

这个错误发生是因为我在同一个包中声明了多次相同的类型。

英文:

This error happened to me because I had a type declared more than once across the same package.

huangapple
  • 本文由 发表于 2017年8月16日 15:18:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/45707324.html
匿名

发表评论

匿名网友

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

确定