github-vet / github-vet/rangeloop-pointer-findings
reusee/jdshopinfo: m.go; 84 LoC
- Dominant language
- No language data
- Stars
- 0
- Forks
- 0
- PR merge metrics
- PR metrics pending
Description
Found a possible issue in [reusee/jdshopinfo](https://www.github.com/reusee/jdshopinfo) at [m.go](https://github.com/reusee/jdshopinfo/blob/0ead15f78ef8785addcec42bc6e5ba81018a9cdd/m.go#L36-L119)
Below is the message reported by the analyzer for this snippet of code. Beware that the analyzer only reports the first
issue it finds, so please do not limit your consideration to the contents of the below message.
> range-loop variable keyword used in defer or goroutine at line 111
[Click here to see the code in its original context.](https://github.com/reusee/jdshopinfo/blob/0ead15f78ef8785addcec42bc6e5ba81018a9cdd/m.go#L36-L119)
Click here to show the 84 line(s) of Go which triggered the analyzer.
```go
for _, keyword := range keywords {
var shopIds []int
get_shop_id:
err := db.Select(&shopIds, `SELECT shop_id FROM shops
WHERE name = $1`,
keyword)
ce(err, "get shop id")
var shopId int
if len(shopIds) == 0 {
db.MustExec(`INSERT INTO shops (name) VALUES ($1)`, keyword)
goto get_shop_id
} else {
shopId = shopIds[0]
}
pt("shop %d %s\n", shopId, keyword)
skus := make(map[string]bool)
var skusLock sync.Mutex
processInfos := func(infos []Info) {
tx := db.MustBegin()
for _, info := range infos {
skusLock.Lock()
skus[info.WareId] = true
skusLock.Unlock()
tx.MustExec(`INSERT INTO items
(sku, shop_id, category, added_date)
VALUES ($1, $2, $3, $4)
ON CONFLICT (sku) DO NOTHING`,
info.WareId,
shopId,
info.Cid1+","+info.Cid2+","+info.Catid,
date,
)
price, err := strconv.ParseFloat(info.JdPrice, 64)
ce(err, "parse price %s", info.JdPrice)
info.Good = strings.Replace(info.Good, "%", "", -1)
info.Good = strings.Replace(info.Good, "暂无评价", "0", -1)
if len(info.TotalCount) == 0 {
info.TotalCount = "0"
}
tx.MustExec(`INSERT INTO infos
(sku, date, good_rate, price, comments, title, image_url)
VALUES ($1, $2, $3, $4, $5, $6, $7)
ON CONFLICT (sku, date) DO UPDATE
SET good_rate = $3, price = $4, comments = $5,
title = $6, image_url = $7`,
info.WareId,
date,
info.Good,
price,
info.TotalCount,
info.Wname,
info.LongImgUrl,
)
}
ce(tx.Commit(), "commit")
}
infos, total, err := GetKeywordPage(keyword, 1)
ce(err, "get infos")
processInfos(infos)
maxPage := (total / 10) + 1
wg := new(sync.WaitGroup)
wg.Add(maxPage - 1)
sem := make(chan bool, 8)
for page := 2; page <= maxPage; page++ {
page := page
sem <- true
go func() {
defer func() {
<-sem
wg.Done()
}()
pt("page %d / %d\n", page, maxPage)
infos, _, err = GetKeywordPage(keyword, page)
ce(err, "get infos")
processInfos(infos)
}()
}
wg.Wait()
pt("collected %d items\n", len(skus))
}
```
Leave a reaction on this issue to contribute to the project by classifying this instance as a **Bug** :-1:, **Mitigated** :+1:, or **Desirable Behavior** :rocket:
See the descriptions of the classifications [here](https://github.com/github-vet/rangeclosure-findings#how-can-i-help) for more information.
commit ID: 0ead15f78ef8785addcec42bc6e5ba81018a9cdd
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.