An object of any type can be assigned to an empty interface. For example, we have the following function
func Println(i interface{} ) {
fmt.Println(i)
}
I do realise its an old discussion, but came across the post and wanted to play around with the concept of having arbitrary function func (interface{})
within another function, instead of interface{}
.
I could write a simple implementation, by providing an inline implementation of a function which would accept interface{}
. And we can call this function from within another function
varForGenFunc := func(in interface{}) int {
fmt.Println("type of this object: ",reflect.TypeOf(in))
return 1}
TakeGenericFunc(varForGenFunc, variableForGen)
Going by this, we can write any implementations of func(interface{})
and pass it as parameter to TakeGenericFunc
You can play around with it here:
https://play.golang.org/p/f5UUhyhEx7u