go - Usage of the `import` statement -


can explain me how import statement works ?

for example have type user in myapp/app/models package:

package models  type user struct {     // exportod fields } 

i have type users in 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 following error:

undefined: user

i tried change imports following code:

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

but error:

undefined: "myapp/app/controllers".user

which don't understand either. so, difference between import . "something" , import "something" ? how import model in case ?

each package has set of types, functions, variables, etc. let's call them entities. each entity can either exported (its name start uppercase letter), or unexported (its name start lowercase letter).

a package can access exported entites of package. this, needs import it, make exported entites available package name identifier. example:

import "github.com/revel/revel" 

will exported entites of revel package, available using revel. prefix. in revel.controller, controller type defined in revel package.

you can alias package identifier prefixing import path wanted identifier. example:

import rev "github.com/revel/revel" 

will import revel entites identifier rev. revel.controller becomes rev.controller. useful if have multiple package same name, or package absurdly long name.

as bonus, can import package anonymously, aliasing blank identifier:

import _ "github.com/revel/revel" 

which import package, not give access exported entities. useful things drivers, need import never access. frequent example database drivers, register database/sql package never need access them directly.

and bonus' bonus, can import locally package, aliasing . identifier. exported entites available without identifier, if defined them in same package.

how import packages you. general convention never alias if can manage it, hide package don't need access still need import (database drivers), , that's all. never need import locally package, if tutorials or frameworks simplicity's sake.


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -