I have method that looks like this:
var eventNotDetected = errors.New("Event not detected")
type VoucherUsageEvent struct {
M models.M
}
func detectVoucherUsageEvent(_ uint64, changeset changelog.Changeset) (VoucherUsageEvent, error) {
var event VoucherUsageEvent
if changeset.GetHeader().Table != db.TABLE_NAME {
return event, eventNotDetected
}
switch changeset.(type) {
case changelog.InsertChangeset, changelog.UpdateChangeset:
m, err := models.Map(changeset)
if err != nil {
return event, err
}
return VoucherUsageEvent{m}, nil
default:
return event, eventNotDetected
}
}
The idea is that it can return an object instance or not, depending on the input. If the object instance can't be returned, the function should indicate why it could not be returned (error occurred during mapping, or just not found)
The return signature is a bit weird when the function doesn't return the instance, for example:
return event, eventNotDetected
return event, err
Basically I just use event
because I can't return just nil.
Or is the better way to just return a reference in this case, so I could change the return cases to:
return nil, eventNotDetected
return nil, err