不能使用 append(*pairs, Pair literal) (type Pairs) 作为

我正在编写一个代码来处理组合warehouse[item[batch, qty]]然后[batch, qty]基于batch与 sum of的组合qty。我的代码是:


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


幕布斯6054654
浏览 141回答 1
1回答

潇潇雨雨

坦克的评论,有2个可能的答案:定义pairs为var pairs Pairs哪个在定义Pairs而不是pairs := new(Pairs)哪个在定义*Pairspairs赋值两边的取消引用为:*pairs = append(*pairs, Pair{k, v})所以现在对我来说完整的工作代码是:package mainimport "fmt"type Inventory struct { //instead of: map[string]map[string]Pairs&nbsp; &nbsp; Warehouse string&nbsp; &nbsp; Item&nbsp; &nbsp; &nbsp; string&nbsp; &nbsp; Batches&nbsp; &nbsp;Pairs}type Pairs []Pairtype Pair struct {&nbsp; &nbsp; Key&nbsp; &nbsp;string&nbsp; &nbsp; Value float64}func main() {&nbsp; &nbsp; fmt.Println("Hello, 世界")&nbsp; &nbsp; var inventory = Inventory{} // or: new(Inventory) noth are working //warehouse[item[batch, qty]]&nbsp; &nbsp; inventory.Warehouse = "DMM"&nbsp; &nbsp; inventory.Item = "Helmet"&nbsp; &nbsp; inventory.Batches = append(inventory.Batches, Pair{"Jan", 10})&nbsp; &nbsp; inventory.Batches = append(inventory.Batches, Pair{"Jan", 30})&nbsp; &nbsp; inventory.Batches = append(inventory.Batches, Pair{"Feb", 30})&nbsp; &nbsp; fmt.Printf("%v\n", inventory)&nbsp; &nbsp; result := inventory.Batches.group()&nbsp; &nbsp; fmt.Printf("%v %T\n", result, result)}func (p *Pairs) group() Pairs {&nbsp; &nbsp; sum := make(map[string]float64)&nbsp; &nbsp; pairs := new(Pairs)&nbsp; &nbsp; // var pairs Pairs&nbsp; &nbsp; for _, el := range *p {&nbsp; &nbsp; &nbsp; &nbsp; sum[el.Key] = sum[el.Key] + el.Value&nbsp; &nbsp; }&nbsp; &nbsp; for k, v := range sum {&nbsp; &nbsp; &nbsp; &nbsp; *pairs = append(*pairs, Pair{k, v}) // with pairs := new(Pairs)&nbsp; &nbsp; &nbsp; &nbsp; // pairs = append(pairs, Pair{k, v})&nbsp; &nbsp;// var pairs Pairs&nbsp; &nbsp; }&nbsp; &nbsp; return *pairs}输出是:Hello, 世界{DMM Helmet [{Jan 10} {Jan 30} {Feb 30}]}[{Jan 40} {Feb 30}] main.PairsProgram exited.
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go