英文:
Special package names in Go
问题
当导入一个名为qp
的自定义子包时,使用以下代码:
"pkgname/qp"
编译器会报错:
... imported and not used: "pkgname/qp"
... undefined: qp
所以显然它似乎对包名qp
有问题,因为当我将其更改为
qp "pkgname/qp"
错误就消失了。
是否有任何“保留”或无效的包名?为什么会发生这种情况?
英文:
When importing a custom sub-package named qp
with
"pkgname/qp"
the compiler complains with
... imported and not used: "pkgname/qp"
... undefined: qp
so obviously it seems to have a problem with the package name qp
, because when I change it to
qp "pkgname/qp"
the errors are gone.
Are there any "reserved" or invalid package names? Why is this happening?
答案1
得分: 6
我猜测在qp包中的文件不以一行代码开始,该行代码为
package qp
它们可能指定了不同的包名。当导入一个包时,包声明中的名称是可用的;如果它与导入路径不匹配,就会出现这样的错误。
换句话说,你导入了"pkgname/qp",但编译器却称其为其他名称,因为你在包声明中称其为其他名称。你的代码中没有引用somethingElse,所以这个导入是未使用的。然后你引用了qp,但它在任何地方都没有定义(尽管看起来是有定义的),因为"pkgname/qp"没有定义qp。
英文:
I'm going to guess that the files in package qp don't start with a line that says
package qp
They probably specify a different package name. When a package is imported, the name in the package declaration is the one that is available under; if that doesn't match the import path, you can get errors like this.
In other words, you imported "pkgname/qp", but the compiler is calling it something else, because you called it something else in the package statement. You don't refer to somethingElse in your code, so the import is unused. Then you refer to qp, which isn't defined anywhere (even though it looks like it was), because "pkgname/qp" doesn't define qp.
答案2
得分: 2
唯一具有特殊含义的包名是 "main"、"C" 和以 "_something" 结尾的包名(用于测试和特定平台的代码)。
您应该展示您声称由于名称而产生错误的代码。
英文:
The only package names with special meanings are "main", "C" and those ending in "_something" (tests and platform specific code).
You should show the code which you claim produces the error due to name.
答案3
得分: 1
qp
是一个有效的包名。例如,
package main
import "fmt"
import "local/qp"
func main() { fmt.Println(qp.QP()) }
编译并且运行没有错误。
英文:
A package name of qp
is valid. For example,
package main
import "fmt"
import "local/qp"
func main() { fmt.Println(qp.QP()) }
compiles and runs without error.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论