Entity and repository implementation

Hi,

I designed an app that depends on DB entity and repository as shown below. However, I am not sure if my design is acceptable as far as Golang goes?

Thanks

Usage

repo := repository{// db goes here}
.... := repo.findOneByID(// uuid goes here)
.... := repo.insert(// entity goes here)

entity.go

package user

type entity struct {
	uuid string
	name string
}

repository.go

package user

type repository struct {
	db *sql.DB
}

func (r repository) findOneByID(id string) (*entity, err) {
	// Execute `SELECT` query here
	// ...
	return entity, err
}

func (r repository) insert(e *entity) (string, error) {
	// Execute `INSERT` query here
	// ...
	return uuid, err
}
1 Like

It looks fine to me. I would not return a pointer to entity. It’s so small and light, you may return the entity as a value.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.