英文:
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 "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 from security lib):
package security
import "gitlab.sio.com/go/zlog"
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论