如何在Go中从多个表构造对象

I'm trying to find the best way to work with objects represented by multiple database tables.

For database access I wrapped sqlx with my own type and then defined some methods:

type DB struct {
    *sqlx.DB
}
func (db *DB) GetSomething(id uint32) *Something {} 
func (db *DB) AddSomething(*Something) {}

It works nice for objects that map one-to-one with single database table. I see a couple of ways to work with multi-table objects:

  1. Define methods on *DB that do selects from multiple tables and then construct an object. Save is similiar.
  2. Create a layer on top of *DB which would construct an object.
  3. Do not construct an object, use its parts.

I rather prefer number 2, but need help on how to design it in Go.

If just to reduce the duplication of code each time you want User, put that somewhere that deals with users. UserService if you will, along with createUser and isUserNameAvailable. Some would go even further and create separate layer just for data retrieval and persistence (DAO layer), for me that's a bit too much of boilerplate.

Sometimes you'll find that you probably don't need a separate method at all. You're pulling a complex report from the DB, but the only time you need it is to return via single rest endpoint. The only time you need user by username and password hash is during login - just use it in unside the login(username,password) method. In those cases, just call sqlx directly in your business methods.