Cannot use append(*pairs, Pair literal) (type Pairs) as type *Pairs in assignment

I’m writing a code to handle the combination of warehouse[item[batch, qty]] then group the [batch, qty] based on batch with sum of qty. my code is:

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)
}

But I got the mentioned error while grouping:

# _/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

I got the answer, there are 2 potential answers:

  1. Define pairs as var pairs Pairs which is defining Pairs instead of pairs := new(Pairs) which is defining *Pairs

  2. Dereference pairs at both sides of the assignment to be: *pairs = append(*pairs, Pair{k, v})

So the full working code for me now is:

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
}

And the output is:

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

Program exited.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.