为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

Go
幕布斯6054654 2022-06-06 17:49:58
我正在编写一个代码来处理组合warehouse[item[batch, qty]]然后[batch, qty]基于batch与 sum of的组合qty。我的代码是:package mainimport "fmt"type Inventory struct {   //instead of: map[string]map[string]Pairs    Warehouse string    Item      string    Batches   Pairs} type Pairs []Pairtype 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 assignmentProgram exited: exit status 2
查看完整描述

1 回答

?
潇潇雨雨

TA贡献1833条经验 获得超4个赞

坦克的评论,有2个可能的答案:

  1. 定义pairsvar pairs Pairs哪个在定义Pairs而不是pairs := new(Pairs)哪个在定义*Pairs

  2. 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.


查看完整回答
反对 回复 2022-06-06
  • 1 回答
  • 0 关注
  • 139 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信