Exploring Reflection with SetString for Structs
Reflection provides powerful tools for manipulating Go structures dynamically. In this example, we encounter a common issue when attempting to set the value of a struct field using reflection: CanSet() always returns false. This hindrance prevents field modifications, leaving us in a quandary.
Identifying the Pitfalls
The provided code snippet highlights two fundamental errors:
Resolving the Issues
After addressing these pitfalls, we can refine our code:
func SetField(source interface{}, fieldName string, fieldValue string) {
v := reflect.ValueOf(source).Elem() // Obtain the value of the pointed object
fmt.Println(v.FieldByName(fieldName).CanSet())
if v.FieldByName(fieldName).CanSet() {
v.FieldByName(fieldName).SetString(fieldValue)
}
}
In the modified SetField() function, we:
Code in Action
With these modifications, the code now successfully updates the Field1 value:
func main() {
source := ProductionInfo{}
source.StructA = append(source.StructA, Entry{Field1: "A", Field2: 2})
fmt.Println("Before: ", source.StructA[0])
SetField(&source.StructA[0], "Field1", "NEW_VALUE")
fmt.Println("After: ", source.StructA[0])
}
Output:
Before: {A 2} true After: {NEW_VALUE 2}
The result showcases the successful modification of Field1 within the Entry struct.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3