I have the following code snippet that after going some reflection, it sets a struct's field to a string value
switch fType := v.(type) {
case MyCompositeFlagString:
s, ok := userInput.(string)
if !ok {
log.Printf("Erroneous input type:%T and input value: %v\n", userInput, userInput)
return ErrUnexpectedInput
}
valueField := values.Elem().Field(i).FieldByName("MyFlagString").FieldByName("Value")
valueField.SetString(s)
I don't see any SetSlice
method in reflect
package.
How can I perform the above operation when the valueField
is of type []string
?
Value.SetString()
is a convenience method for setting string
values. There isn't a separate method for all types for obvious reasons, but there is a "generic" Value.Set()
method, you may use that. You just have to obtain a reflect.Value
from the value you want to set:
var someSlice ...
valueField.Set(reflect.ValueOf(someSlice))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.