特殊的Go包名称

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

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.

huangapple
  • 本文由 发表于 2013年2月20日 00:21:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/14962288.html
匿名

发表评论

匿名网友

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

确定