I need map the struct to create a JSON structure. The collector_id attribute in JSON should be able to take null value or int value.
I hace the follow code:
type purchaseInfo struct {
CollectorID *int64 `json:"collector_id"`
}
func mapPurchaseInfo(collectorID int64) purchaseInfo {
var collectorIDToSend *int64
if collectorID < 0 {
collectorIDToSend = nil
} else {
collectorIDToSend = collectorID
}
return purchaseInfo{
CollectorID: collectorIDToSend,
}
}
This code doesn’t compile, the collectorID can’t be assigned to collectorIDToSend.
Is there a way to be able to do this?
Thanks!
2
Answers