我正在编写一个代码来处理组合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
潇潇雨雨
相关分类