日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達者為師

網(wǎng)站首頁 編程語言 正文

GoFrame框架使用避坑指南和實踐干貨_Golang

作者:王中陽Go ? 更新時間: 2022-08-03 編程語言

gf gen dao

生成dao層的腳手架工具很好用,我遇到的坑是這樣的:

生成的dao文件和同事們的不一致,生成文件成功,但是對應(yīng)的Columns是空的,雖然有這個方法,但是方法內(nèi)沒有值。我的版本比同事們的略高,我一直以為是這個原因,各種降級和同事保持一致的版本后還是不行。

最終發(fā)現(xiàn):是配置文件中連接的數(shù)據(jù)庫不對。

因為沒有連接到數(shù)據(jù)庫,所以取不到列值;但是因為配置文件中約定了表名,文件正常生成了。

好坑。

設(shè)置參數(shù)可不傳

使用 ...interface{}

func GetXxx(xxx ...interface{}) { }

model作為結(jié)構(gòu)體類型

當(dāng)我們的業(yè)務(wù)比較復(fù)雜,需要更新多個關(guān)聯(lián)表時,可以把需要修改的表統(tǒng)一定義到一個結(jié)構(gòu)體中,而不是想到一個model處理一個model。

這種思想比較好,把關(guān)聯(lián)的model統(tǒng)一封裝到一個結(jié)構(gòu)體中,很清晰,也能避免有遺漏。

type GoodsRelevantItem struct {
   Shop        *model.Shops
   Brand       *model.GoodsBrand
   Desc        *model.GoodsDescription
   Cover       []*model.GoodsCover
   Attributes  []*model.GoodsAttributes
   Goods       *model.Goods
   DisCategory []*model.DisCategory
}

使用with關(guān)聯(lián)取值而不是join

能用with的一定用with關(guān)聯(lián)取值,而不是用join。

發(fā)現(xiàn)自己整理的栗子沒有官網(wǎng)的好,大家還是看官網(wǎng)吧: goframe.org/pages/viewp…

不使用結(jié)構(gòu)體批量添加數(shù)據(jù)

goframe非常靈活,插入的數(shù)據(jù)可以是結(jié)構(gòu)體也可以是map,也可以是map類型的切片,來實現(xiàn)批量添加。

主程序如下:

//主圖輪播
goodsImgs := []map[string]interface{}{} //圖片集
for k, img := range gomeGoods.MainImgs {
   goodsImg := map[string]interface{}{} //圖片
   goodsImg["cover"] = "http:" + img
   goodsImg["goods_id"] = gconv.Int(goodsDetail["goods_id"])
   goodsImg["sort"] = k
   goodsImgs = append(goodsImgs, goodsImg)
}
//添加主圖
err = m.AddGoodsCover(ctx, tx, goodsImgs)
if err != nil {
   err = errors.New("添加商品主圖失敗")
   return err, 0
}

gomeGoods.MainImgs的定義:

MainImgs []string `json:"mainImgs"`

插入數(shù)據(jù)

batch指定了批量插入時一次插入的條數(shù)

func (m *goodsMessageService) AddGoodsCover(ctx context.Context, tx *gdb.TX, goodsImgs g.List) (err error) {
   if len(goodsImgs) == 0 {
      return errors.New("暫無數(shù)據(jù)")
   }
   _, err = dao.GoodsCover.TX(tx).Ctx(ctx).Batch(len(goodsImgs)).Insert(goodsImgs)
   checkErr(err, "UpdateGoodsCover")
   return
}

原文鏈接:https://juejin.cn/post/7081959981456556068

欄目分類
最近更新