检查结构体字段是否为空。

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

Check if struct fields are not empty

问题

如何迭代结构中的字段并检查它们是否为空,而不需要显式操作?

我有一个示例结构体:

type Example struct {
    Foo, Bar string
}

当然,我可以显式地检查每个字段是否不是空字符串,但我不想以这种方式做。有没有简单的方法可以实现我所需的功能?

英文:

How to iterate over the fields in a structure and check if they are not empty without explicit?

I have an example structure

type Example struct {
	Foo, Bar string
}

I can ofc check explicitly if every field is not "", but I dont want to do that this way. Is there any simple way to accomplish what I need?

答案1

得分: 5

如果想要检查所有字段是否具有零值,你可以简单地进行如下检查:

var foo Example
if foo == (Example{}) {
  // ...
}

Go语言会将所有结构体字段初始化为零值(对于字符串类型来说,是空字符串"")。请注意,如果结构体具有不可比较类型的字段(例如指针、切片或映射),这种方法可能不会按预期工作。

你可以使用reflect.Value的方法来迭代字段并比较值(就像reflect.DeepEqual()所做的那样)。但在大多数情况下,最好是编写一个明确执行检查的函数。

英文:

If want to check if all fields have the zero value you could simply check

var foo Example
if foo == (Example{}) {
  // ...
}

go initializes all struct fields with the zero value (in case of a string the empty string ""). Keep in mind that this probably not works as expected if the struct has fields with not comparable types (i.e. of type pointer, slice or map).

You can use the methods of reflect.Value to iterate the fields and compare the values (like reflect.DeepEqual() does). But in most cases it is best to simply write a function which explicitly does the check.

答案2

得分: 4

我建议您使用reflect包,正如mkopriva所建议的那样。只需编写一次代码,然后在需要的所有结构体中使用它。
例如,我适应了在这里找到的代码:

func IsEmpty(object interface{}) (bool, error) {
    // 首先检查常规的空定义
    if object == nil {
        return true, nil
    } else if object == "" {
        return true, nil
    } else if object == false {
        return true, nil
    }
    // 然后查看它是否是一个结构体
    if reflect.ValueOf(object).Kind() == reflect.Struct {
        // 创建一个空的结构体对象副本进行比较
        empty := reflect.New(reflect.TypeOf(object)).Elem().Interface()
        if reflect.DeepEqual(object, empty) {
            return true, nil
        } else {
            return false, nil
        }
    }
    return false, errors.New("未实现此结构体的检查")
}
英文:

I suggest you to use reflect package, as suggested mkopriva. Write it once and use it for all struct that you need.
For example, i adapted a scratch found here

func IsEmpty(object interface{}) (bool, error) {
//First check normal definitions of empty
if object == nil {
	return true, nil
} else if object == "" {
	return true, nil
} else if object == false {
	return true, nil
}
//Then see if it's a struct
if reflect.ValueOf(object).Kind() == reflect.Struct {
	// and create an empty copy of the struct object to compare against
	empty := reflect.New(reflect.TypeOf(object)).Elem().Interface()
	if reflect.DeepEqual(object, empty) {
		return true, nil
	} else {
		return false, nil
	}
}
return false, errors.New("Check not implementend for this struct")  }

huangapple
  • 本文由 发表于 2021年8月2日 19:49:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/68621079.html
匿名

发表评论

匿名网友

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

确定