域和数据库之间的映射

I am creating application which get the data from the database and pass them to frontend as JSON.

I think it can be a good idea to create separate structs for the data which I get from the database and the data which I pass to REST services. I am right or it is my mistake ?

In this case I need to map the structs from one layer to another. I do it in the database layer now:

func (ds *DataStore) AddUnit(_unit models.Unit) {

    unit := Unit{}
    unit.Name = _unit.Name
    unit.Description = _unit.Description
    db.Create(&unit)
}

func (ds *DataStore) UpdateUnit(id int, _unit models.Unit) models.Unit {

    unit := Unit{}
    db.First(&unit, id)
    unit.Name = _unit.Name
    unit.Description = _unit.Description
    db.Save(&unit)

    return _unit
}

Now I have this code in each method for each entity in my code. May be there is some way or library to extract this mapping functionality and describe it using declarative way in the separate file ?

You could look for, and use a library; the problem with libs is that they are trying to cater to unknown problems. The author of the library doesn't know how it will be used. This can, in some situations, make them much slower, less flexible, and more complicated that just writing the code yourself.

To start you should extract the duplicate logic from the DataStore:

// extract the logic of copying across the fields to a function
func setFields(u Unit, unit models.Unit) Unit {
    u.Name = unit.Name
    u.Description = unit.Description
    return u
}

func (ds *DataStore) AddUnit(_unit models.Unit) {
    // trimmed ...
    unit := setFields(Unit{}, _unit)
    // trimmed ...
}

func (ds *DataStore) UpdateUnit(id int, _unit models.Unit) models.Unit {
    unit := Unit{}
    db.First(&unit, id)

    unit = setFields(unit, _unit)

    // trimmed ...
}

You may then also consider extracting the function call of setFields to a new interface:

type unitSetter interface {
   setFields(u Unit, unit models.Unit) Unit
}

And add this to the DataStore to make the code more extensible, reusable, and testable.

type DataStore struct {
    // trimmed ...
    units unitSetter
}

func (ds *DataStore) AddUnit(_unit models.Unit) {
    // trimmed ...
    unit := ds.units.setFields(Unit{}, _unit)
    // trimmed ...
}