有没有可能不指定包名? [英] Is it possible don't specify package name?

查看:121
本文介绍了有没有可能不指定包名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的代码示例:

Here is an example of my code:

package main

import (
  "./bio"
)

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

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

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

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

推荐答案

您可以使用 import declaration like:

You could use as an import declaration like:

. "./bio"




如果显式句点(<$ c $出现而不是名称,则在该包的包数据块中声明的所有数据包的导出标识符将在导入源文件的文件块中声明,并且必须在没有限定符的情况下被访问。 strong>。

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.

这就是 govey这样的测试框架

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() code>因为以''开始的导入。

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

不要滥用它,因为,正如 twotwotwo 评论 风格指南 阻止它在测试之外。 $ b

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


除了这一种情况,不要在你的程序中使用 import。

它使得程序难以阅读,因为不清楚像Quux这样的名称是当前软件包或导入中的顶级标识符ed包。

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 通过 Simon Whitehead 进行了评论,使用相对导入通常不被视为最佳做法(请参阅 Go语言包结构)。

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

您还应该通过 GOPATH 而不是相对的方式导入包,如导入并未使用错误

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

这篇关于有没有可能不指定包名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆