202208月总结

2022-08-30  本文已影响0人  Newzer

一:本月已完成
0.完成从本地开发到线上独立部署的过程
1.熟悉 go,基本业务可以用 go 实现
2.整理了一个基本框架搭建,基础功能有:集成配置文件,日志分割,gorm2, redis, kafka, jwt,该框架可以直接作为下个全新项目的开始
3.单元测试和基准测试初体验,后面会在实践中慢慢尝试

二:下月待完成

  1. 租车服务开发并实现项目多实例部署
    2.实现网关到多实例这个转发的过程
    3.自己搭建时序数据库

三:可以写进简历的东西
1.kafka
2.taos 时序数据库
3.阿里云 amqp 消息队列

四:go 语言知识小积累(小技巧)
1.struct,map , slice 和 array 都可以用复合字面值的方式初始化

  1. gorm tag 运用 每种类型选一个字段
    type Combo struct {
    ID string gorm:"primary_key;type:char(26)" json:"id"
    Name string json:"name" gorm:"index comment:套餐名称 type:varchar(255);not null"
    ShopId string json:"shop_id" gorm:"index; type:char(26); comment:商户id;not null"
    ProductId string json:"product_id" gorm:"type:char(26);comment:商品id"
    RuleId int json:"rule_id" gorm:"comment:分成规则id"
    Category int json:"category" gorm:"type:tinyint; comment:套餐类别,1:单租车 2:单租电 3:车+电"
    Price float64 json:"price" gorm:"type:decimal(5,2); comment:价格"
    TotalNum int json:"total_num" gorm:"type:int(11); comment:总数量"
    IsDeleted int json:"is_deleted" gorm:"type:tinyint; default:0; comment:软删除字段"
    Detail string json:"detail" gorm:"type:text; comment:套餐描述"
    Model
    }
    type Model struct {
    CreatedAt *time.Time json:"created_at"
    UpdatedAt *time.Time json:"updated_at"
    }

  2. gin 参数绑定
    type ComboList struct {
    ShopId string json:"shop_id" binding:"required" //必选字段
    Name string json:"name,omitempty" binding:"omitempty" //可选字段
    Status int json:"status" binding:"omitempty,oneof=0 1 2 3 4"//可选的枚举字段
    PageSize int json:"page_size" binding:"omitempty" //可选字段
    Price float64 json:"price,omitempty" binding:"omitempty,gte=0"//可选的 大于等于0
    }
    需要注意的是 json标签里的 omitempty 和 binding 里的omitempty作用是不同的
    json 里的 omitempty 表示在参数绑定的时候,如果这个参数没有传过来则该结构体被赋值的时候该字段就不存在,如果没有这个 omitempty,绑定后该字段存在,但值为该字段类型的零值
    binding 里的 omitempty 表示客户端在发出请求的时候可以不传这个字段

4.在模型中进行更新数据时,最好用 map 的格式,因为 struct 格式时,当为零值时数据库不更新
最全查询:
db.Debug().Model(combo).Limit(params.PageSize).Where(where).Where("is_deleted = 0").Where("name like ?", params.Name+"%").Where("id > ?", params.LastId).Find(&combos)
Debug() 会把 sql 打印出来
Model() 指定相关模型
Limit() 查询条数限制
Where() 查询条件,可以多个链接,最好用 占位符 ?
Find() 指定查询后绑定的结构体,若一个表有20个字段,但列表查询时只需要4个字段,就可以把这4个字段放到一个结构体中然后把该结构体的实例放到Find()里

  1. 结构体转化为map(下面 params 是结构体)
    where := map[string]interface{}{}
    b, _ := json.Marshal(params)
    json.Unmarshal(b, &where)

6.删掉 map 的某个字段
delete(where, "last_id")

7.string 转化为 int. strconv.Atoi()
int 转化为string strconv.ItoA()

8.字符串 time.Time 和时间戳之间的转化
字符串 ------> time.Time 用time.Parse()
time.Time. --------> 字符串 用Time.Format()

time.Time -------->时间戳 用Time.Unix()
时间戳 ---------> time.Time() 用time.Unix()

上一篇 下一篇

猜你喜欢

热点阅读