0

Can someone explain me how the import statement works ?

For example I have a type User in the myapp/app/models package:

package models

type User struct {
    // exportod fields
}

I have a type Users in the myapp/app/controllers package:

package controllers

import (
    _ "myapp/app/models"
    "github.com/revel/revel"
)

type Users struct {
    *revel.Controller
}

func (c Users) HandleSubmit(user *User) revel.Result {
    // Code here
}

This gives me the following error:

undefined: User

I tried to change the imports to the following code:

import (
    . "advorts/app/models"
    "github.com/revel/revel"
)

But get this error:

undefined: "myapp/app/controllers".User

Which I don't understand either. So, what is the difference between import . "something" and import "something" ? How to properly import my model in my case ?

Elwinar
  • 9,103
  • 32
  • 40
cnaize
  • 3,139
  • 5
  • 27
  • 45
  • `import . "something"` if you are familiar with Python is similar to `from something import *`. In the most basic sense you are importing the contents of the "something" package into the current namespace, so rather than `something.NewThing()`, it is simply `NewThing()`. It is generally avoided, though the test framework Ginkgo makes use of it in a pretty valid way IMHO. As far as your import issue with your layout, I would suggest re-reading docs on how to layout a Go project. – sberry Jun 26 '14 at 07:48

1 Answers1

10

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.

Elwinar
  • 9,103
  • 32
  • 40