I have 20+ functions that return either a struct or nil. I need to go through all of them and if they return a struct I append it to a slice of structs. So I would like to know if there is a way to iterate through all the functions and append the result if it is not nil because checking the result for every function seems to be a waste of time. Could anyone suggest a way to do this? Maybe an example or something.
So, I know you kind of got the answer in the comments, but thought I'd put an example in anyway:
funcs := []func()*struct{Thing int}{
func()*struct{Thing int}{return nil},
func()*struct{Thing int}{
newStruct := struct{Thing int}{Thing: 1}
return &newStruct
},
}
sliceOfStructs := []struct{Thing int}{}
for _,f := range funcs {
res := f()
if res != nil {
sliceOfStructs = append(sliceOfStructs, *res)
}
}