简体   繁体   中英

How to cast interface implementation pointer to interface pointer?

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.

Using

type Env struct {
    db store.Datastore
}

is the correct solution. Because store.Datastore is interface type, it is already pointer and youre not coping any data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM