Struct is not iterable in Go. Also you want to iterate thorough a foo
attribute not through a multiple StructB
fields. Therefore you should iterate through a slice which is an attribute of the struct. And then just check for equation to find a desired value or determine that it is not there.
Playground:
target := "C"
a := StructB{[]StructA{StructA{"A", "B", "C"}}}
for _, i := range a.foo {
if target == i.varA {
fmt.Println(i.varA)
} else if target == i.varB {
fmt.Println(i.varB)
} else if target == i.varC {
fmt.Println(i.varC)
} else {
fmt.Println("None of above")
}
}
Go is pretty explicit and tricks seldom give a real profit.