最近在学习Golang, 刚从Java转过来, 之前在SpringBoot写接口时, service层常常需要entity数据库类转换为dto, 之前在Java我会使用Mapstruct库, 确实很方便, 写几个接口就完成了
然后在go写接口的时候, 就找了一下有没有类似Mapstruct这样的库, 没找到这么方便的, 不过找到一个Copier库, 用起来也还不错
安装
go get github.com/jinzhu/copier
使用

这是我在logic层的一个方法, 我认为是类似于springboot 中service实现类的一个方法, 在这里我将数据库模型entity批量转成了业务模型dto
copier.Copy支持单个/批量转换
单个对象复制:
使用 copier.Copy(&target, &source) 来复制一个对象
批量复制:
使用 copier.Copy(&target, &source) 来批量复制对象数组(或切片)
正文完