GoLang – types from different scopes

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

GoLang - types from different scopes

问题

如果你遇到了如下错误:

interface conversion: interface {} is *purchase.User, not *purchase.User (types from different scopes)

看起来你发送的类型是正确的,因为错误消息中的期望类型和实际类型完全相同。

英文:

If you are getting an error like:

interface conversion: interface {} is *purchase.User, not *purchase.User (types from different scopes)

It appears that you are sending the correct type since the expected and actual type in the error message are exactly the same.

答案1

得分: 1

你可能在另一个包(purchase2)中创建了另一种指向*purchase.User的类型,并且这是你的方法正在寻找的类型,但你却发送了(purchase.User)。

例如:

purchase包中:

type User struct {
	firstName string
	lastName string
}

purchase2包中:

import purchase

type User purchase.User

请检查你所引用的类型。

英文:

You may have created another type pointing to the *purchase.User in another package (purchase2) and that is the type your method is looking into receiving but you are sending (purchase.User)

For example:

in purchase package

type User struct {
	firstName string
	lastName string
}

in purchase2 package

import purchase

type User purchase.User

Check which type are you referring to.

答案2

得分: 0

这可能是由于你以两个不同的导入路径导入了同一个包,导致了这个问题。除非在特殊情况下(如AST代码重写、非标准的依赖管理等),否则很少见到这种情况。

如果你正在使用不同的路径导入同一个包,那么很有可能你正在做一些非常错误的事情。停止这样做吧 GoLang – types from different scopes

英文:

Alternate cause for this: you have somehow imported the package twice, with two different import paths. This is unlikely to be seen except in unusual circumstances (AST code rewriters, non-standard vendoring, etc).

If you are importing the same package with different paths, there is a very good chance that you're doing something very wrong. Stop it GoLang – types from different scopes

huangapple
  • 本文由 发表于 2022年3月21日 17:24:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/71555308.html
匿名

发表评论

匿名网友

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

确定