/golang/src/myproject/controllers/default.go": MODIFY|ATTRIB
2016/04/26 18:52:27 [INFO] Start building...
# myproject/controllers
controllers/default.go:14: newline in string
controllers/default.go:15: syntax error: unexpected c at end of statement
2016/04/26 18:52:27 [ERRO] ==============Build failed ===================
2016/04/26 18:52:28 [SKIP] " mycode/golang/src/myproject/controllers/default.go": MODIFY|ATTRIB
如果注意到 这个情况,相当于即时编译,能够立刻看到错误。
vim ../controllers/default.go
vim ../views/index.tpl
目前看起来MVC的结构非常清晰,效率是不是很高?开发效率也许会上来很多?至少感觉挺直接的,标准的php or web mvc的基本概念。
index.tpl {{}}其中包括属性。controls。data[‘’]=xxxx
package controllers
import (
"github.com/astaxie/beego"
)
type MainController struct {
beego.Controller
}
func (c *MainController) Get() {
c.Data["Website"] = "eego.me"
c.Data["Email"] = "ie@gmail.com"
c.Data["Dictionary"]="my dictionary is here"
c.TplName = "index.tpl"
}
<!DOCTYPE html>
<html>
<head>
{{.Dictionary}}
</div>
</header>
<footer>
<div class="author">
Official website:
<a href="http://{{.Website}}">{{.Website}}</a> /
如果是提供app的API功能,通过or mapping模块,可以直接完全产生好标准的访问 model,controller
Note that the new API application doesn’t have a static
and views
folder.
You can also create model and controller based on the database schema by providing database conn:
bee api [appname] [-tables=""] [-driver=mysql] [-conn=root:@tcp(127.0.0.1:3306)/test]
-tables: a list of table names separated by ',' (default is empty, indicating all tables)
-driver: [mysql | postgres | sqlite] (default: mysql)
-conn: the connection string used by the driver, the default is ''
e.g. for mysql: root:@tcp(127.0.0.1:3306)/test
e.g. for postgres: postgres://postgres:postgres@127.0.0.1:5432/postgres
If 'conn' argument is empty, bee api creates an example API application,
when 'conn' argument is provided, bee api generates an API application based
on the existing database.
The command 'api' creates a folder named [appname] and inside the folder deploy
the following files/directories structure:
├── conf
│ └── app.conf 配置
├── controllers
│ └── object.go
│ └── user.go //控制器名字和model一样
├── routers
│ └── router.go //route非常重要
├── tests
│ └── default_test.go
├── main.go
└── models
└── object.go
└── user.go
var db *sql.DB func init() { db, _ = sql.Open("mysql", "root:@tcp(127.0.0.1:3306)/test?charset=utf8") db.SetMaxOpenConns(2000) db.SetMaxIdleConns(1000) db.Ping() }
go的mysql 连接字符串,
"root:@tcp(127.0.0.1:3306)/test?charset=utf8"
"root:@tcp(127.0.0.1:3306)/test?charset=utf8"
数据库连接
数据库连接使用datebase/sql Open函数进行连接
db, err := sql.Open("mysql", "user:password@tcp(localhost:5555)/dbname?charset=utf8")
其中连接参数可以有如下几种形式:
user@unix(/path/to/socket)/dbname?charset=utf8
user:password@tcp(localhost:5555)/dbname?charset=utf8
user:password@/dbname
user:password@tcp([de:ad:be:ef::ca:fe]:80)/dbname