返回结构体或错误的函数

huangapple go评论93阅读模式
英文:

Function that is returning struct or error

问题

我有一个类似这样的方法:

var eventNotDetected = errors.New("未检测到事件")

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

这个方法的思路是根据输入来决定是否返回对象实例。如果无法返回对象实例,函数应该指示无法返回的原因(映射过程中发生错误,或者对象未找到)。

当函数不返回实例时,返回的签名有点奇怪,例如:

return event, eventNotDetected
return event, err

基本上,我只是使用event,因为我不能只返回nil

或者在这种情况下,更好的方法是返回一个引用,这样我可以将返回情况更改为:

return nil, eventNotDetected
return nil, err
英文:

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

答案1

得分: 0

是的,你可以将输出参数定义为指针*VoucherUsageEvent

func detectVoucherUsageEvent(_ uint64, changeset changelog.Changeset) (*VoucherUsageEvent, error)

可以返回引用或nil。

return &VoucherUsageEvent{m}, nil

或者

return &event, eventNotDetected

或者

return nil, err
英文:

Yes you can, define the out parameter as pointer *VoucherUsageEvent.

func detectVoucherUsageEvent(_ uint64, changeset changelog.Changeset) (*VoucherUsageEvent, error)

Return as reference or nil.

return &VoucherUsageEvent{m}, nil

OR

return &event, eventNotDetected

OR

return nil, err

huangapple
  • 本文由 发表于 2017年6月23日 11:13:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/44712481.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定