AST analysis: How can I tell, at runtime, what datatypes a `function(emptyInterface) emptyInterface` expects and returns?

Hello fellow gophers !

You are given a function (which you cannot change, it is a function variable, you cannot read its code) that takes in an interface and returns an interface:


You know that this function will always convert its input to a data type X (every possible input gets converted to that same X), and will always returned an object of type Y (always type Y) converted to interface{}.

How can you tell, (using reflection ?), at runtime, what are the exact types X and Y. Is it even possible to do so without running the function, just by programmatically analyzing its code ?

Any ideas ? Think outside the box, or inside the box, whatever works.

Come on people, come up with some ideas.

Sample code

package main

import (

func test(x interface{}) {
xType := reflect.TypeOf(x)
xValue := reflect.ValueOf(x)
fmt.Println(xType, xValue) // “[]int [1 2 3]”

func main() {
test([]int{1, 2, 3})

Hi Yamil,

It is a nice try, but requires me to change the function. It also requires me to be able to read and edit its code.

I am looking for more creative ideas, where I receive the function as a variable (the function itself it is my input) and then I analyze it to figure out where are the lines with return and what exactly is returned on those lines.

I don’t really understand your problem so i am just thinking and writing maybe this wont be help at all but…

Go is strict language try smth like this function(inputInterface{}) outputInterface{}
create your interfaces