douhe5092 2016-05-04 23:58
浏览 31
已采纳

简洁的nil检查结构字段指针?

Say I have this struct:

type Foo struct {
    Bar        *string          `json:"bar"`
    Baz        *int64           `json:"baz,omitempty"`
    Qux        *string          `json:"qux"`
    Quux       string           `json:"quux"`
}

After unmarshalling the json, I check for nil like so:

switch {
case f.Bar == nil:
    return errors.New("Missing 'bar'")
case f.Baz == nil:
    f.Baz = 42
case f.Qux == nil:
    return errors.New("Missing 'qux'")
}

(or through a series of if statements, etc...)

I understand that I can put all the nil comparisons in one comma separated case, but each nil check will have differing returns.

My question: is there a less verbose way of doing the nil checks?

  • 写回答

1条回答 默认 最新

  • duanpi7107 2016-05-05 08:07
    关注

    A question to you: how less verbose you want to get? Because you want to do different things on different conditions (different fields being nil). Your code contains these different things and the different conditions. Beyond that what's "redundant" in your code are just the switch and case keywords. You want to leave them out? Because the rest is not "redundant", they are required.

    Also note that in Go cases do not fall through even without a break (unlike in other languages), so in your above example if f.Baz is nil, you will set it to 42 and f.Qux will not be checked (so no error will be returned), but if f.Baz is non-nil and f.Qux is nil, an error will be returned. I know it's just an example, but this is something to keep in mind. You should handle errors first if you use a switch! Or use if statements and then the error will be detected and returned regardless of the order of field checks.

    Your code with switch is clean and efficient. If you want to make it less verbose, readability (and performance) will suffer.

    You may use a helper function which checks if a pointer value is nil:

    func n(i interface{}) bool {
        v := reflect.ValueOf(i)
        return v.Kind() == reflect.Ptr && v.IsNil()
    }
    

    And using it:

    func check(f *Foo) error {
        switch {
        case n(f.Bar):
            return errors.New("Missing 'bar'")
        case n(f.Qux):
            return errors.New("Missing 'qux'")
        case n(f.Baz):
            x := int64(42)
            f.Baz = &x
        }
        return nil
    }
    

    Or using if statements:

    func check2(f *Foo) error {
        if n(f.Bar) {
            return errors.New("Missing 'bar'")
        }
        if n(f.Qux) {
            return errors.New("Missing 'qux'")
        }
        if n(f.Baz) {
            x := int64(42)
            f.Baz = &x
        }
        return nil
    }
    

    Try these on the Go Playground.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题