0

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.

1

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh, I did not know about it. It seems. that it is not obvious thing. I have read stackoverflow.com/a/27178682/4167563 it for now. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.