Is it possible to specify a package name?

Here's 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 the package name:

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

      

?

+3


source to share


1 answer


You can use as an import declaration , for example:

. "./bio"

      

If an explicit period ( .

) is displayed instead of a name , all exported IDs declared in that package of package packages will be declared in the file block of the original import file and must be accessible without a qualifier .

This is what a testing framework like govey is :

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 .

.



Don't overuse it as, as twotwotwo comments , the style guide discourages it outside of tests.

Except for one case, don't use import .

in your programs.
This makes programs difficult to read because it is unclear whether a name such as Quux is a top-level identifier in the current package or in an imported package.

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


As Simon Whitehead commented , using relative imports is generally considered best practice (see, for example, Go Language Pack Structure ).

You must also import the package via GOPATH

instead of relative, as shown in " Import and don't use error ".

+8


source







All Articles