I have a interface:
type Datastore interface {
InsertString(str string) error
}
I have PostgreSql implementation of this interface:
type DB struct {
session *sql.DB
}
func (db *DB) InsertString(str string) error {
return nil
}
func NewDB(user, password, dbName string) (*DB, error) {
dbinfo := fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable",
user, password, dbName)
db, err := sql.Open("postgres", dbinfo)
if err != nil {
log.Println(err)
return nil, err
}
err = db.Ping()
if err != nil {
log.Println("Cannot ping database")
return nil, err
}
return &DB{db}, nil
}
Now I want to get store and use it in my API handlers.
So, I create Environment structure and try to use it:
type Env struct {
db *store.Datastore
}
func (env *Env) HealthCheckHandler(w http.ResponseWriter, r *http.Request) {
// A very simple health check.
}
func main() {
log.Println("Server staring...")
db, _ := store.NewDB(USER, PASSWORD, DB_NAME)
env := Env {
db: db,
}
r := mux.NewRouter()
r.HandleFunc("/health", env.HealthCheckHandler)
log.Fatal(http.ListenAndServe("localhost:"+PORT, r))
}
But I have a error:
cannot use db (type *store.DB) as type *store.Datastore in field value: *store.Datastore is pointer to interface, not interface
I know, how to fix this. I can use this structure instead of initial:
type Env struct {
db store.Datastore
}
But I don't want to copy the data.