I understand what the issue is, I am not providing a pointer address when I am trying to scan the fields from my database call - however in this situation I am not quite sure how to work around it.
I have a struct that looks like this:
type ItemsRequest struct {
Sku string `json:"sku"`
Name string `json:"name"`
Barcode string `json:"barcode,omitempty"`
Category struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Subcategories []interface{} `json:"subcategories,omitempty"`
CategoryPosition int `json:"categoryPosition,omitempty"`
ResourceURI string `json:"resource_uri,omitempty"`
} `json:"category,omitempty"`
CategoryPosition int `json:"categoryPosition,omitempty"`
LongDesc string `json:"longDesc,omitempty"`
MinQty int `json:"minQty,omitempty"`
MultQty int `json:"multQty,omitempty"`
UnitPrice string `json:"unitPrice,omitempty"`
ImageURLs []string `json:"imageURLs,omitempty"`
Variants []interface{} `json:"variants,omitempty"`
AdditionalPrices map[string]string `json:"additionalPrices,omitempty"`
audtdatetime int
}
The AdditionalPrices
field is meant to represent an object of values, e.g:
{
'UK': "12.34",
'AU': "14.00"
}
As such, I'm fairly confident that the map[string]string
type for the field is correct...
I am retrieving data to populate this struct from my database and then trying to scan the results into an instance of this struct. In my query, I have a predefined number of columns for which I want to populate AdditionalPrices
with.
As such I started off with this:
func getItemsFromDB() []*ItemsRequest {
rows, err := bcdataDBConnection.Query(`
SELECT *
FROM vProducts
WHERE max_audit_date_time > @AuditTime
`, sql.Named("AUDITTIME", 2016101412000000))
if err != nil {
log.Fatal("There was an issue with the query for items: ", err)
}
defer rows.Close()
var items []*ItemsRequest
for rows.Next() {
var item = &ItemsRequest{}
err := rows.Scan(
&item.Sku,
&item.Name,
&item.Category.ID,
&item.Category.Name,
&item.Category.CategoryPosition,
&item.LongDesc,
&item.Barcode,
&item.UnitPrice,
&item.MinQty,
&item.MultQty,
&item.CategoryPosition,
&item.AdditionalPrices["NZBASE"],
&item.AdditionalPrices["NZWWW"],
&item.AdditionalPrices["MMCMFR"],
&item.AdditionalPrices["MMBASE"],
&item.AdditionalPrices["AUBASE"],
&item.AdditionalPrices["MMCMAU"],
&item.AdditionalPrices["AUDSA"],
&item.AdditionalPrices["AUDVIC"],
&item.AdditionalPrices["AUDWWW"],
&item.audtdatetime,
)
if err != nil {
log.Fatal("Scan error: ", err)
}
items = append(items, item)
}
return items
}
However this didn't appear to be correct syntax and so I changed it to:
func getItemsFromDB() []*ItemsRequest {
rows, err := bcdataDBConnection.Query(`
SELECT *
FROM vHandshakeProducts
WHERE max_audit_date_time > @AuditTime
`, sql.Named("AUDITTIME", 2016101412000000))
if err != nil {
log.Fatal("There was an issue with the query for items: ", err)
}
defer rows.Close()
var items []*ItemsRequest
for rows.Next() {
var item = &ItemsRequest{}
err := rows.Scan(
&item.Sku,
&item.Name,
&item.Category.ID,
&item.Category.Name,
&item.Category.CategoryPosition,
&item.LongDesc,
&item.Barcode,
&item.UnitPrice,
&item.MinQty,
&item.MultQty,
&item.CategoryPosition,
item.AdditionalPrices["NZBASE"],
item.AdditionalPrices["NZWWW"],
item.AdditionalPrices["MMCMFR"],
item.AdditionalPrices["MMBASE"],
item.AdditionalPrices["AUBASE"],
item.AdditionalPrices["MMCMAU"],
item.AdditionalPrices["AUDSA"],
item.AdditionalPrices["AUDVIC"],
item.AdditionalPrices["AUDWWW"],
&item.audtdatetime,
)
if err != nil {
log.Fatal("Scan error: ", err)
}
items = append(items, item)
}
return items
}
Which my IDE likes more, however upon running it I get the error:
2018/01/15 15:35:06 Scan error: sql: Scan error on column index 11: destination not a pointer
So at this point, I'm not quite sure how to populate the values for AdditionalPrices
from my database call.
What do I need to do to populate the AdditionalPrices
map with the values I get from the database?
As per the keys in my code, I already know the map keys for which I would like to assign the values to.