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.

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

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题