Each package has a set of types, functions, variables, etc. Let's call them entities. Each entity can be either exported (its name start with an Uppercase letter), or unexported (its name start with a lowercase letter).
A package can only access the exported entites of another package. To do this, it needs to import
it, which will make the exported entites available with the package name as identifier. Example:
import "github.com/revel/revel"
will get all exported entites of the revel
package, which will be available using revel.
prefix. As in revel.Controller
, which is the Controller
type defined in the revel
package.
You can alias a package identifier by prefixing the import path with the wanted identifier. Example:
import rev "github.com/revel/revel"
will import all revel
entites with the identifier rev
. So revel.Controller
becomes rev.Controller
. It is useful if you have multiple package with the same name, or a package with an absurdly long name.
As a bonus, you can import a package anonymously, by aliasing it to the blank identifier:
import _ "github.com/revel/revel"
which will import the package, but not give you access to the exported entities. It is useful for things like drivers, which you need to import but never access. A frequent example is the database drivers, which register themselves to the database/sql
package so you never need to access them directly.
And as a bonus' bonus, you can also import locally a package, by aliasing it with the .
identifier. The exported entites will then be available without identifier, as if you defined them in the same package.
How to properly import your packages is up to you. The general convention is to never alias if you can manage it, to hide the package that you don't need to access but still need to import (database drivers), and that's all. You really never need to import locally a package, even if some tutorials or frameworks do it for simplicity's sake.