"color": "green" property is present in 2nd struct even though database field doesn't have it. It should be "color": "".
This happens when rows.Scan is used in a loop using same struct as a receiver which is then copied to a slice. json.Unmarshal doesn't reset struct fields from previous iteration to default values.
Suggestion: reset dst to default value before calling json.Unmarshal
Issue: suppose database table has 2 columns:
varchar(255)
jsonb
when reading this table in a loop to a slice of
User
structsfinal result would be:
"color": "green"
property is present in 2nd struct even though database field doesn't have it. It should be"color": ""
.This happens when
rows.Scan
is used in a loop using same struct as a receiver which is then copied to a slice.json.Unmarshal
doesn't reset struct fields from previous iteration to default values.Suggestion: reset
dst
to default value before callingjson.Unmarshal