Here is my code:
type ICacheEngine interface {
// ...
}
// implements all methods of ICacheEngine
type RedisCache struct { }
type ApplicationCache struct {
Cache *ICacheEngine
}
func NewRedisCache() *ApplicationCache {
appCache := new(ApplicationCache)
redisCache := new(RedisCache)
appCache.Cache = redisCache // here is an error : can not use *RedisCache as *ICacheEngine
return appCache
}
RedisCache
implements all methods of ICacheEngine
. I can pass RedisCache
to the method which get ICacheEngine
:
func test(something ICacheEngine) *ICacheEngine {
return &something
}
....
appCache.Cache = test(redisCache)
But I cannot assign RedisCache
to ICacheEngine
. Why ? How can I avoid test()
function ? And what will be looking programming with interfaces when I set concrete type to interface and next call it methods ?