是否有可能不指定包名?

时间:2014-11-08 10:33:14

标签: go

以下是我的代码示例:

package main

import (
  "./bio"
)

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

是否可以使用我的paxkage(bio)中的函数而无需指定包名称:

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

1 个答案:

答案 0 :(得分:8)

您可以像import declaration一样使用:

. "./bio"
  

如果显示明确的句点(.)而不是名称,则在该程序包的程序包块中声明的所有程序包的导出标识符将在导入源文件中声明'必须在没有限定符的情况下访问文件块和

这就是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)
            })
        })
    })
}

您不需要使用convey.So()convey.Convey(),因为导入的开头是&{39; .'。

不要滥用它,因为twotwotwo comments style guide 会在测试之外阻止它。

  

除了这一情况外,请勿在您的程序中使用import .   它使程序更难以阅读,因为不清楚Quux之类的名称是当前包中还是导入包中的顶级标识符。

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


commented作为Simon Whitehead,使用相对导入通常不被视为最佳做法(例如参见" Go language package structure&#34 )。

您还应该通过GOPATH而非相对来导入包,如" Import and not used error"中所示。

相关问题