英文:
Check if a slice contains a struct with a given field value
问题
尝试检查某个切片中的结构体是否包含给定字段的值,所以我写了这个代码。
func main() {
//测试
Objs := []Obj{{1,"xxx"},{2,"yyy"},{3,"zzz"}}
res := containsStructFieldValue(Objs,"X",1)
fmt.Println(res)
}
type Obj struct {
X int
Y string
}
func containsStructFieldValue(slice []Obj ,fieldName string,fieldValueToCheck interface {}) bool{
for _,s := range slice{
r := reflect.ValueOf(s)
f := r.FieldByName(fieldName)
if f.IsValid(){
if f.Interface() == fieldValueToCheck{
return true //存在具有给定值的字段
}
}
}
return false
}
我希望它适用于任何给定的结构体类型,但当我尝试将参数设置为slice []interface{}
时,发现这是不可能的。有没有办法使上述方法适用于任何结构体类型?
英文:
Trying to check if a struct in some slice contains a value of a given field so i wrote this
func main() {
//test
Objs := []Obj{{1,"xxx"},{2,"yyy"},{3,"zzz"}}
res := containsStructFieldValue(Objs,"X",1)
fmt.Println(res)
}
type Obj struct {
X int
Y string
}
func containsStructFieldValue(slice []Obj ,fieldName string,fieldValueToCheck interface {}) bool{
for _,s := range slice{
r := reflect.ValueOf(s)
f := r.FieldByName(fieldName)
if f.IsValid(){
if f.Interface() == fieldValueToCheck{
return true //a field with the given value exists
}
}
}
return false
}
i need it to work for any given struct type but when i tried slice []interface
as the parameter i found out that its not possible, any idea on how to make the above method work for any struct type?
答案1
得分: 7
你可以使用reflect
来遍历interface{}
,例如:
func containsStructFieldValue(slice interface{}, fieldName string, fieldValueToCheck interface{}) bool {
rangeOnMe := reflect.ValueOf(slice)
for i := 0; i < rangeOnMe.Len(); i++ {
s := rangeOnMe.Index(i)
f := s.FieldByName(fieldName)
if f.IsValid() {
if f.Interface() == fieldValueToCheck {
return true
}
}
}
return false
}
请注意,我没有检查slice
是否确实是一个切片...如果不是,这段代码会引发恐慌。如果你想避免这种行为,你可以使用reflect.Kind
来进行检查。
英文:
You can use reflect
to range over an interface{}
, for instance:
func containsStructFieldValue(slice interface{} ,fieldName string,fieldValueToCheck interface {}) bool{
rangeOnMe := reflect.ValueOf(slice)
for i := 0; i < rangeOnMe.Len(); i++ {
s := rangeOnMe.Index(i)
f := s.FieldByName(fieldName)
if f.IsValid(){
if f.Interface() == fieldValueToCheck {
return true
}
}
}
}
Note that I did not check that slice
is indeed a slice... If not, this code will panic. You can use reflect.Kind
to check this if you want to avoid this behaviour.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论