【发布时间】:2019-07-20 18:32:54
【问题描述】:
我从源 A 获取数据并将其存储在一个结构切片中,如下所示:
type ProductPrice struct {
Type string
Sku string
UnitPrice string
PriceList string
standardPrice string
specialPrice string
specialStart string
specialEnd string
pricingUnit string
categoryCode string
isOnSpecial bool
}
func getProductPricesFromDatabase(instance string) []ProductPrice {
rows, err := myDBConnection.Query(// My query here)
if err != nil {
log.Fatal("There was an issue with the query for product price: ", err)
}
defer rows.Close()
var productPrices []ProductPrice
for rows.Next() {
var product = ProductPrice{}
err := rows.Scan(
&product.Type,
&product.Sku,
&product.standardPrice,
&product.specialPrice,
&product.specialStart,
&product.specialEnd,
&product.pricingUnit,
&product.PriceList,
&product.categoryCode,
)
if err != nil {
log.Fatal("product price scan error: ", err)
}
productPrices = append(productPrices, product)
}
return productPrices
}
然后我从源 B 获取一些数据并将其存储在一个结构切片中,如下所示:
type ContractProductPrice struct {
CustID string
PriceBy string
AppliesTo string
PriceList string
StartDate string
EndDate string
PricingAdjustmentType string
PricingAdjustmentValue string
UseLowest string
}
func getContractProductPricesFromDatabase(instance string) []ContractProductPrice {
rows, err := myDBConnection.Query(// My query here)
if err != nil {
log.Fatal("There was an issue with the query for contract product price: ", err)
}
defer rows.Close()
var contractProductPrices []ContractProductPrice
for rows.Next() {
var product = ContractProductPrice{}
err := rows.Scan(
&product.CustID,
&product.PriceBy,
&product.AppliesTo,
&product.PriceList,
&product.StartDate,
&product.EndDate,
&product.PricingAdjustmentType,
&product.PricingAdjustmentValue,
&product.UseLowest,
)
if err != nil {
log.Fatal("contract product price scan error: ", err)
}
contractProductPrices = append(contractProductPrices, product)
}
return contractProductPrices
}
从源 B 获取数据后,我想用源 B 的一些数据更新源 A 的结构切片。
productPrices := getProductPricesFromDatabase(instance)
contractProductPrices := getContractProductPricesFromDatabase(instance)
processedProductPrices := processProductPricesFromDatabase(productPrices, contractProductPrices)
func processProductPricesFromDatabase(productPrices []ProductPrice, contractProductPrices []ContractProductPrice) []ProductPrice {
// Loop over contact prices and update relevant product prices
for _, contractPrice := range contractProductPrices {
for _, product := range productPrices {
if contractPrice.AppliesTo == product.Sku {
product.UnitPrice = contractPrice.PricingAdjustmentValue
}
}
}
return productPrices
}
但是,运行后,processedProductPrices 中的单价仍然为空。
通过搜索,我了解问题所在; Go 按值传递,因此我不会更新原始内存地址,因此值不会改变。 但是,我不明白/不知道我需要改变什么来解决这个问题,因为我正在使用一片结构而不是一个更简单的数字/字符串片等示例。
如何更新productPrices,以便在我返回它时,processedProductPrices 等于更新后的productPrices 结构切片?
【问题讨论】:
-
你需要循环
[]*ProductPrice,即指针,以便能够修改它们,否则你在循环中看到的是你已经知道的每个切片元素的副本。或者使用索引productPrices[i].UnitPrice = ...。 -
@mkopriva 你能给我举个例子吗?我不确定如何重构您的建议?
-
@mkopriva 您的 cmets 属于答案。