jeudi 6 août 2020

Dynamically set a struct field to a slice value using reflect

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 ?





Aucun commentaire:

Enregistrer un commentaire