`import`语句的用法 [英] Usage of the `import` statement
问题描述
有人可以向我解释 import
语句是如何工作的吗?例如我有一个 myapp / app / models
包中的$ c> User :
pre > 包模型
类型用户结构{
//导出字段
}
在 myapp / app / controllers
Users >包:
包控制器
导入(
_myapp / app / models
github.com/revel/revel
)
类型用户struct {
* revel.Controller
}
func(c Users)HandleSubmit(user * User)revel.Result {
//此处代码
}
这给了我以下错误:
undefined:User $
我尝试将导入更改为以下代码:
导入(
。advorts / app / models
github.com/revel/revel
)
但是得到这个错误:
undefined:myapp / app / controllers.User
我也不明白。那么, import的区别是什么? something
和导入something
?如何正确导入
我的模型在我的情况?
有一组类型,函数,变量等。让我们称它们为实体。每个实体都可以被导出(其名称以大写字母开头)或未导出(名称以小写字母开头)。
包只能访问导出另一包装的物品。要做到这一点,它需要 import
它,这将使导出的实体可以使用包名作为标识符。示例:
importgithub.com/revel/revel
将获得 您可以通过在导入路径前添加所需的标识符来为包标识符设置别名。示例: 将导入所有 作为奖励,您可以通过别名匿名导入包它到了空白标识符: 它将导入包,但不会让您访问导出的实体。这对于需要导入但不能访问的驱动程序非常有用。一个常见的例子是数据库驱动程序,它们将自己注册到 作为额外的奖励,您还可以在本地导入一个包,并使用 如何正确导入包是由您决定的。一般惯例是永远别名,如果你可以管理它,隐藏你不需要访问但仍然需要导入的包(数据库驱动程序),就这些了。你真的从不需要在本地导入一个包,即使有些教程或框架是为了简单起见。 Can someone explain me how the For example I have a type I have a type This gives me the following error: I tried to change the imports to the following code: But get this error: Which I don't understand either. So, what is the difference between 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 will get all exported entites of the You can alias a package identifier by prefixing the import path with the wanted identifier. Example: will import all As a bonus, you can import a package anonymously, by aliasing it to the blank identifier: 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 And as a bonus' bonus, you can also import locally a package, by aliasing it with the 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. 这篇关于`import`语句的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! revel
包的所有导出实体,这些包将可用 revel 。
前缀。在 revel.Controller
中,它是 revel $中定义的
Controller
类型c $ c> package。
import revgithub.com/revel/revel
revel
entite,其标识符为 rev
。因此 revel.Controller
变为 rev.Controller
。如果您有多个具有相同名称的包或具有荒谬长名称的包,它非常有用。
import _github.com/revel/revel
数据库/ sql
包,因此您不需要直接访问它们。
。
标识符进行别名。然后,导出的实体将无需标识符即可使用,就像您将它们定义在同一个包中一样。
import
statement works ?User
in the myapp/app/models
package:package models
type User struct {
// exportod fields
}
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
}
undefined: User
import (
. "advorts/app/models"
"github.com/revel/revel"
)
undefined: "myapp/app/controllers".User
import . "something"
and import "something"
? How to properly import
my model in my case ?import
it, which will make the exported entites available with the package name as identifier. Example:import "github.com/revel/revel"
revel
package, which will be available using revel.
prefix. As in revel.Controller
, which is the Controller
type defined in the revel
package.import rev "github.com/revel/revel"
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.import _ "github.com/revel/revel"
database/sql
package so you never need to access them directly..
identifier. The exported entites will then be available without identifier, as if you defined them in the same package.