You are faced with a very common misconception for newcomers to Go: an empty interface interface{} does not mean "any type". This is actually not the case. Go is statically typed. The empty interface {} is the actual (strongly typed type), for example, for example. string or struct{Foo int} or interface{Explode() bool} .
This means that if something is of type interface{} , it is of this type, not "any type."
Your function
func test(x func() interface{})
accepts one parameter. This parameter is (a function without parameters) that returns a specific type, type interface{} . You can pass any function to test that matches this signature: "There are no parameters and return interface{} ". None of your functions a and b match this signature.
As stated above: interface {} not a magic abbreviation for "any", it is a separate static type.
You have to change, for example. a:
func a() interface{} { return "hello" }
Now this may look strange when you return a string that is not of type interface{} . This works because any type is assigned to variables of type interface{} (since each type has at least no methods :-).
source share