go 学习

最近开始学习go,这里记下学习中遇到的问题。

主要目标是利用go做一个rest的后台接口,前台利用pb把数据展示出来。

rest接口利用 github.com/ant0ine/go-json-rest/rest

数据库利用 github.com/jinzhu/gorm

打算先把接口做起来,前台能够展示出来,利用以上两个库基本上能实现目标,但是在查询一个表时,暴露给前台的时候,有一个字段始终是空的,费了好一番功夫才发现,是数据库表命名的不统一,通过查看gorm文档发现其表名是,把首字母自动转换成大写,其余是小写,这样当定义的type经过自动转换后的字段名称和实际不一致时,就出了问题,差不到数据,不过文档中也提供了另一种,重写表名的方法。具体如下。

// Overriding Column Name
type Animal struct {
    AnimalId    int64     `gorm:"column:beast_id"`         // set column name to `beast_id`
    Birthday    time.Time `gorm:"column:day_of_the_beast"` // set column name to `day_of_the_beast`
    Age         int64     `gorm:"column:age_of_the_beast"` // set column name to `age_of_the_beast`
}

  

posted @ 2017-01-24 20:51  mybuilder  阅读(239)  评论(0编辑  收藏  举报