I am trying to iterate over a slice of interfaces to find my specific struct by id and change the attribute.
type A struct {
ID ID
Steps []Step
}
type Step interface{}
type B struct {
ID ID
}
type C struct {
ID ID
}
func (s *A) findStepByID(id ID) (Step, error) {
for index, step := range s.Steps {
switch stepType := step.(type) {
case A:
if stepType.ID == id {
return step, nil
}
case B:
if stepType.ID == id {
return step, nil
}
default:
return nil, errors.New("no step found")
}
}
return nil, errors.New("no step found")
}
When I found my struct for example B
then I will set B.ID = xy