gpt4 book ai didi

go - 不能将append(* pairs,Pairliteral)(Pairs)类型用作* Pairs在分配中

转载 作者:行者123 更新时间:2023-12-01 22:44:19 29 4
gpt4 key购买 nike

我正在编写一个代码来处理warehouse[item[batch, qty]]的组合,然后将基于[batch, qty]batchqty的总和分组。我的代码是:

package main

import "fmt"

type Inventory struct { //instead of: map[string]map[string]Pairs
Warehouse string
Item string
Batches Pairs
}
type Pairs []Pair
type Pair struct {
Key string
Value float64
}

func main() {
fmt.Println("Hello, 世界")
var inventory = Inventory{} // or: new(Inventory) noth are working //warehouse[item[batch, qty]]
inventory.Warehouse = "DMM"
inventory.Item = "Helmet"
inventory.Batches = append(inventory.Batches, Pair{"Jan", 10})
inventory.Batches = append(inventory.Batches, Pair{"Jan", 30})
inventory.Batches = append(inventory.Batches, Pair{"Feb", 30})
fmt.Printf("%v\n", inventory)
inventory.Batches.group()
}

func (p *Pairs) group() {
sum := make(map[string]float64)
pairs := new(Pairs)
for _, el := range *p {
sum[el.Key] = sum[el.Key] + el.Value
}
fmt.Printf("%v %T\n", sum, sum)
for k, v := range sum {
pairs = append(*pairs, Pair{k, v}) // <--------------- here is the error
}
fmt.Printf("%v %T\n", pairs, pairs)
}

但是我在分组时遇到了提到的错误:
# _/C_/Users/HASAN~1.YOU/AppData/Local/Temp/present-048467841
.\prog.go:36:9: cannot use append(*pairs, Pair literal) (type Pairs) as type *Pairs in assignment

Program exited: exit status 2

最佳答案

意见箱,有2个潜在答案:

  • pairs定义为定义var pairs PairsPairs,而不是定义pairs := new(Pairs)*Pairs
  • 分配两侧的pairs取消引用为:*pairs = append(*pairs, Pair{k, v})

  • 所以现在对我来说完整的工作代码是:

    package main

    import "fmt"

    type Inventory struct { //instead of: map[string]map[string]Pairs
    Warehouse string
    Item string
    Batches Pairs
    }
    type Pairs []Pair
    type Pair struct {
    Key string
    Value float64
    }

    func main() {
    fmt.Println("Hello, 世界")
    var inventory = Inventory{} // or: new(Inventory) noth are working //warehouse[item[batch, qty]]
    inventory.Warehouse = "DMM"
    inventory.Item = "Helmet"
    inventory.Batches = append(inventory.Batches, Pair{"Jan", 10})
    inventory.Batches = append(inventory.Batches, Pair{"Jan", 30})
    inventory.Batches = append(inventory.Batches, Pair{"Feb", 30})
    fmt.Printf("%v\n", inventory)
    result := inventory.Batches.group()
    fmt.Printf("%v %T\n", result, result)
    }

    func (p *Pairs) group() Pairs {
    sum := make(map[string]float64)
    pairs := new(Pairs)
    // var pairs Pairs
    for _, el := range *p {
    sum[el.Key] = sum[el.Key] + el.Value
    }
    for k, v := range sum {
    *pairs = append(*pairs, Pair{k, v}) // with pairs := new(Pairs)
    // pairs = append(pairs, Pair{k, v}) // var pairs Pairs
    }
    return *pairs
    }

    输出为:
    Hello, 世界
    {DMM Helmet [{Jan 10} {Jan 30} {Feb 30}]}
    [{Jan 40} {Feb 30}] main.Pairs

    Program exited.

    关于go - 不能将append(* pairs,Pairliteral)(Pairs)类型用作* Pairs在分配中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62196479/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com