I pass data to a func with input type interface.
This code:
main(){
SampleData := Input{
Recipients: []string{"abc","efg"},
Msg: string("Test message"),
}
InsertInSendTBL(SampleData)
}
type Input struct {
Recipients []string
Msg string
sender string
}
type Output struct {
Recipients []string
Msg string
reciver string
}
func InsertInSendTBL(Data interface{}) {
DataInput := reflect.ValueOf(Data)
NewVal := Output{
Recipients: DataInput.FieldByName("Recipients").([]string{}),//LINE ERORE
Msg: DataInput.FieldByName("Msg").String(),
sender: "1000",
}
}
One of my struct fields is slice of string. I searched in reflect package but not found anything so I use ".([]string{})" . Result is this error:
invalid type assertion: DataInput.FieldByName("Recipients").(composite literal) (non-interface type reflect.Value on left)
so I try interface in reflect But another error
cannot use DataInput.FieldByName("Recipients").Interface() (type interface {}) as type []string in field value: need type assertion