可以不指定包名吗?

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

Is it possible don't specify package name?

问题

这是我的代码示例:

package main

import (
  "./bio"
)

func main() {
   bio.PeptideEncoding(genome, codonTable)
}

是否可以在不指定包名的情况下使用我的包(bio)中的函数:

func main() {
   PeptideEncoding(genome, codonTable)
}

英文:

Here is an example of my code:

package main

import (
  "./bio"
)

func main() {
   bio.PeptideEncoding(genome, codonTable)
}

Is it possible to use functions from my paxkage (bio) without specifying package name:

func main() {
   PeptideEncoding(genome, codonTable)
}

?

答案1

得分: 8

你可以像这样使用导入声明

. "./bio"

如果一个明确的句点(.)出现在名称的位置,那么该包块中声明的该包的所有导出标识符将被声明在导入源文件的文件块中,并且必须在没有限定符的情况下访问

这就是像 govey 这样的测试框架所做的

package package_name

import (
    "testing"
    . "github.com/smartystreets/goconvey/convey"
)

func TestIntegerStuff(t *testing.T) {
    Convey("给定一个起始值的整数", t, func() {
        x := 1

        Convey("当整数递增时", func() {
            x++

            Convey("值应该增加一", func() {
                So(x, ShouldEqual, 2)
            })
        })
    })
}

由于导入以句点(.)开头,你不需要使用 convey.So()convey.Convey()

不过不要滥用这种方式,因为正如 twotwotwo 评论的那样,样式指南 不鼓励在测试之外使用它。

除了这种情况外,请不要在你的程序中使用 import .。这会使程序更难阅读,因为不清楚像 Quux 这样的名称是当前包中的顶级标识符还是导入包中的标识符。

这就是为什么我提到了一个使用这种技术的测试框架。


正如 Simon Whitehead 评论的那样,使用相对导入通常不被认为是最佳实践(例如参见“Go 语言包结构”)。

你还应该通过 GOPATH 导入包,而不是使用相对路径,如“导入但未使用的错误”所示。

英文:

You could use as an import declaration like:

. "./bio"

> If an explicit period (.) appears instead of a name, all the package's exported identifiers declared in that package's package block will be declared in the importing source file's file block and must be accessed without a qualifier.

That is what a testing framework like govey does:

package package_name

import (
    "testing"
    . "github.com/smartystreets/goconvey/convey"
)

func TestIntegerStuff(t *testing.T) {
    Convey("Given some integer with a starting value", t, func() {
        x := 1

        Convey("When the integer is incremented", func() {
            x++

            Convey("The value should be greater by one", func() {
                So(x, ShouldEqual, 2)
            })
        })
    })
}

You don't need to use convey.So(), or convey.Convey() because of the import starting with a '.'.

Don't abuse it though, since, as twotwotwo comments, The style guide discourages it outside of tests.

> Except for this one case, do not use import . in your programs.
It makes the programs much harder to read because it is unclear whether a name like Quux is a top-level identifier in the current package or in an imported package.

That is why I mentioned a testing framework using this technique.


As commented by Simon Whitehead, using relative import is not generally considered as the best practice (see for instance "Go language package structure").

You should also import the package via the GOPATH instead of relatively, as show in "Import and not used error".

huangapple
  • 本文由 发表于 2014年11月8日 18:33:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/26816151.html
匿名

发表评论

匿名网友

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

确定