-3
func GetResult (service interface{}) {
   switch v := service.(type) {
     case services.Account:
       service = service.(services.Account)
     default:
       service = service.(Mock_Account)
    }
   res, err := service.GetAccount()
 }

It says service is type of interface doesn't have any method. There is typecasting is not working any Idea how to call GetAccount method?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Anupam Somani
  • 224
  • 1
  • 7
  • 17
  • 6
    Go is statically typed so you can't change the type of `service`, and there is no type casting in go. If you only want to call `GetAccount`, why not accept an interface that only has that method? – JimB May 31 '18 at 17:28

1 Answers1

3

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.

Adrian
  • 42,911
  • 6
  • 107
  • 99