亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

不能使用 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 關注
  • 172 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號