doushu5451 2014-06-26 07:43
浏览 83
已采纳

`import`语句的用法

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 ?

  • 写回答

1条回答 默认 最新

  • dongzou3751 2014-06-26 08:03
    关注

    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.

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

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大