Go uses interfaces to generalize types. Therefore, if you need a function that accepts a specific interface you write
func MyFunction(t SomeInterface) {...}
Each type that satisfies SomeInterface can be passed to MyFunction .
Now SomeInterface might look like this:
type SomeInterface interface { SomeFunction() }
To satisfy SomeInterface , the type implementing it must implement SomeFunction() .
If you need an empty interface ( interface{} ), the object should not implement any method that should be passed to the function:
func MyFunction(t interface{}) { ... }
This function above accepts each type as all types implement an empty interface .
Return type
Now that you can have all possible types, the question arises as to how to return the type back was actually set earlier. An empty interface does not provide any methods, so you cannot name anything of that value.
To do this, you need type statements: let the runtime check if type X is in the value Y and convert it to this type, if so.
Example:
func MyFunction(t interface{}) { v, ok := t.(SomeConcreteType)
In this example, the input parameter t is considered to be SomeConcreteType . If t actually has type SomeConcreteType , v will contain an instance of this type, and ok will be real. Otherwise, ok will be false. See type specifications for details.