Goalng杂碎

记录一些比较基础的


chan 阻塞未完成的携程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package main
import(
"fmt"
"time"
"runtime"
)
var threadNum=100
var threadChannel chan int
func main(){
beginTime := time.Now()
maxProcess := runtime.NumCPU()
runtime.GOMAXPROCS(maxProcess)
threadChannel=make(chan int,threadNum)
for i:=0;i<threadNum;i++{
go Printer(i)
}
for i:=0;i<threadNum;i++{
<-threadChannel
}
finishTime :=time.Now()
fmt.Println("timeDuring",finishTime.Sub(beginTime))
}
func Printer(a int)(){
time.Sleep(2000 * time.Millisecond)
fmt.Printf("Thread num %d\n",a)
threadChannel <- 1
}

事务管理

1
2
3
4
o := NewOrm()
err := o.Begin()// 事务处理过程......// 此过程中的所有使用 o Ormer 对象的查询都在事务处理范围内if SomeError {
err = o.Rollback()} else {
err = o.Commit()}

自定义错误处理 e.g.404

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package controllers
import (
"github.com/astaxie/beego"
)
type ErrorController struct {
beego.Controller
}
func (c *ErrorController) Error404() {
c.Data["content"] = "page not found"
c.TplName = "404.tpl"
}
func (c *ErrorController) Error501() {
c.Data["content"] = "server error"
c.TplName = "501.tpl"
}
func (c *ErrorController) ErrorDb() {
c.Data["content"] = "database is now down"
c.TplName = "dberror.tpl"
}

beego POST JSon数据时候的空值问题

在 API 的开发中,我们经常会用到 JSON 或 XML 来作为数据交互的格式,如何在 beego 中获取 Request Body 里的 JSON 或 XML 的数据呢?

1. 在配置文件里设置 copyrequestbody = true

2. 在 Controller 中
1
2
3
4
5
6
func (this *ObejctController) Post() {
var ob models.Object
json.Unmarshal(this.Ctx.Input.RequestBody, &ob)
objectid := models.AddOne(ob)
this.Data["json"] = "{\"ObjectId\":\"" + objectid + "\"}"
this.ServeJson()}

注释很关键。

beego handler上的注解详解

1
2
3
4
5
6
// @Title getStaticBlock// @Description get all the staticblock by key
// @Param key path string true "The email for login"
// @Success 200 {object} models.ZDTCustomer.Customer
// @Failure 400 Invalid email supplied
// @Failure 404 User not found
// @router /staticblock/:key [get]

首先是 CMSController 定义上面的注释,这个是用来显示这个模块的作用。接下来就是每一个函数上面的注释,这里列出来支持的各种注释:

* @Title

这个 API 所表达的含义,是一个文本,空格之后的内容全部解析为 title

* @Description

这个 API 详细的描述,是一个文本,空格之后的内容全部解析为 Description

* @Param

参数,表示需要传递到服务器端的参数,有五列参数,使用空格或者 tab 分割,五个分别表示的含义如下

* 
    1. 参数名


    2. 参数类型,可以有的值是 formData、query、path、body、header,formData 表示是 post 请求的数据,query 表示带在 url 之后的参数,path 表示请求路径上得参数,例如上面例子里面的 key,body 表示是一个 raw 数据请求,header 表示带在 header 信息中得参数。


    3. 参数类型


    4. 是否必须


    5. 注释



* @Success

成功返回给客户端的信息,三个参数,第一个是 status code。第二个参数是返回的类型,必须使用 {} 包含,第三个是返回的对象或者字符串信息,如果是 {object} 类型,那么 bee 工具在生成 docs 的时候会扫描对应的对象,这里填写的是想对你项目的目录名和对象,例如 models.ZDTProduct.ProductList 就表示 /models/ZDTProduct 目录下的 ProductList 对象。
三个参数必须通过空格分隔

* @Failure

失败返回的信息,包含两个参数,使用空格分隔,第一个表示 status code,第二个表示错误信息

* @router

路由信息,包含两个参数,使用空格分隔,第一个是请求的路由地址,支持正则和自定义路由,和之前的路由规则一样,第二个参数是支持的请求方法,放在 [] 之中,如果有多个方法,那么使用 , 分隔。

beego开启orm反向工程

1
2
3
4
5
6
7
8
9
10
11
12
13
orm.RegisterModel(new(User))
orm.RegisterDriver("mysql", orm.DRMySQL)
orm.RegisterDataBase("default", "mysql", "root:root@/beego_orm_test?charset=utf8")
name := "default"
// drop table 后再建表
force := false
// 打印执行过程
verbose := true
// 遇到错误立即返回
err := orm.RunSyncdb(name, force, verbose)
if err != nil {
fmt.Println(err)
}