1 回答

TA貢獻1829條經驗 獲得超13個贊
您計劃選擇的方式看起來ctx.Done()是正確的。
在我看來,你處理可變狀態的方式是錯誤的。
嘗試這樣的事情:
var state = State{}
select {
case type <- typeChan
stats.Type = type
if (stats.OrderCount != nil) {
return stats
}
case count <- countChan
stats.OrderCount = count
if (stats.Type != nil) {
return stats
}
case <-ctx.Done()
return stats
}
現在你的函數應該是這樣的:
go func() {
orderCount, err := s.Storage.GetOrderCount(ctx, customerUUID)
if err != nil {
return // Here you probably want to have errChan
}
if orderCount == 0 {
countChan <- "NA"
} else {
countChan <- strconv.Itoa(orderCount)
}
}()
這一切都有點粗略,因為您的示例非常復雜,但應該為您提供遵循的方向。
- 1 回答
- 0 關注
- 127 瀏覽
添加回答
舉報