【问题标题】:Loop over a slice of structs and update value遍历一片结构并更新值
【发布时间】: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 属于答案。

标签: pointers go struct slice


【解决方案1】:

每当您处理您知道需要修改的值时,至少在我看来,最好使用指针。它们会让您的生活更轻松。

所以而不是:

func getProductPricesFromDatabase(instance string) []ProductPrice {
    // ...
    var productPrices []ProductPrice

    for rows.Next() {
        var product = ProductPrice{}

        // ...
    }    
    return productPrices
}

我建议您将代码重构为:

func getProductPricesFromDatabase(instance string) []*ProductPrice {
    // ...
    var productPrices []*ProductPrice

    for rows.Next() {
        var product = new(ProductPrice)

        // ...
    }    
    return productPrices
}

现在对getContractProductPricesFromDatabase 执行相同操作,最后将参数类型更新为processProductPricesFromDatabase 函数:

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
}

作为替代方案,如果您想继续使用非指针类型,您可以通过对切片进行索引来直接修改切片引用的值。

func processProductPricesFromDatabase(productPrices []ProductPrice, contractProductPrices []ContractProductPrice) []ProductPrice {
    // Loop over contact prices and update relevant product prices

    for _, contractPrice := range contractProductPrices {

        for i, _ := range productPrices {
            if contractPrice.AppliesTo == productPrices[i].Sku {
                productPrices[i].UnitPrice = contractPrice.PricingAdjustmentValue
            }
        }
    }

    return productPrices
}

【讨论】:

  • 感谢您的指导!完美运行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-15
  • 1970-01-01
  • 2021-06-16
  • 2021-03-08
  • 1970-01-01
  • 2014-04-02
  • 2021-10-16
相关资源
最近更新 更多