go 简介学习

/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"

}


template file,nothing special. it's common template html files,just like any defined templates

<!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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值