To annotate your code sample:
func GetResult (service interface{}) {
// service is of type interface{}
switch v := service.(type) {
case services.Account:
// This does nothing: it assigns the existing value back into service, which is still of type interface{}
// v exists in scope and is of type services.Account, but it's unused.
service = service.(services.Account)
default:
// Ditto
service = service.(Mock_Account)
}
// service is still of type interface{}, which defines no methods
res, err := service.GetAccount()
}
As you can see, the variable service
here is always of type interface{}
. A variable's type cannot change: Go is static typed. However, if you just want to call a method GetAccount
on whatever type you get passed in, this can be done without the type assertions and with full type safety by declaring an interface:
type AccountGetter interface {
GetAccount() (res ResultType, err error)
}
func GetResult(service AccountGetter) {
res,err := service.GetAccount()
}
You can then call GetResult
and pass a services.Account
or a Mock_Account
or any type that implements a GetAccount
method with the appropriate signature.