I need to be able to assign a value to a struct field where I specify the field name in a variable. After searching around I thought the following approach would work but I still get the "type does not support indexing" error. This example is simplified:
type item struct {
red bool
green bool
blue bool
}
func (i *item) setProperty (propName string, propValue bool) *item {
(*i)[propName] = propValue
return i
}
func main() {
item := &item{red: true, green: true}
item = item.setProperty("blue", true)
fmt.Println(item)
}
Is it possible to assign to struct fields where the field name is in a variable? Any suggestions on how I get that to work? Thank you.