I have a program as below. I am trying to change the type of a function argument dynamically as another library I am using requires passing myMethod
signature with concrete type instead of that interface to do proper unmarshalling. Is that even possible in Go to dynamically make a function or anonymous function with argument's type generated dynamically or perhaps change the parameter type of a function?
package main
import (
"fmt"
"reflect"
)
type MyType interface {
doThis()
}
type MyType1 struct{}
func (m MyType1) doThis() {
fmt.Println("Type1 doThis")
}
type MyType2 struct{}
func (m MyType2) doThis() {
fmt.Println("Type2 doThis")
}
func myMethod(myType MyType) {
myType.doThis()
}
func main() {
fmt.Println("Hello, playground")
var type1 MyType
type1 = &MyType1{}
type1Val := reflect.TypeOf(type1)
// TODO - change myMethod signature dynamically to accept type1Val as the type
}
Here is the GoPlay link
Edit: Adding clarification
The library I am using exposes a registerSomething(someFunc)
where the input argument type of someFunc
will be later used in some unmarshaling. If the input argument type is an interface, the unmarshal will return a map. If its a typed struct, the unmarshal will return the typed struct with all params populated correctly so I don't have to deal with unmarshaling.