如何设计数据库服务类

I want to create a class that takes a sql connection, and then I can write methods to return data from my database.

func main() {
  db := dbConnect()
  defer db.Close()

  // pass the db to my dbservice.go somehow?
}

Then in my handlers (for a web app) I want to be able to do:

var userKey, err := dbService.getUserKey(123)

Do I just create a file like dbservice.go and then create structs that represent my return values, and then do:

func (mystruct *MyStruct) GetUserKey(id int64) (key string, err error) {
   //
}

Also, how do I pass the db to this in my main so it can use the database connection?

Have the service have an internal field that takes a pointer to the db and use that.

type DBService struct {
    db *sql.DB
}

func (dbs *DBService) GetUserKey(id int64) (key string, err error)
    dbs.db.Query("SOMETHING")
    return
}

func NewService(db *sql.DB) *DBService { return &DBService{db: db} }