dongsou8980 2014-11-08 10:33
浏览 21
已采纳

是否可以不指定软件包名称?

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条回答 默认 最新

  • dpwdldgn43486 2014-11-08 10:35
    关注

    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".

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算