type RoleHasPermissionList struct { model.Roles Rolehaspermissions []CUS_Rolehaspermissions `json:"rolehaspermissions" gorm:"foreignkey:role_id; references:id"` } type CUS_Rolehaspermissions struct { PermissionId int `json:"-"` RoleId int `json:"-"` PermissinInfo model.Permission `gorm:"foreignkey:id; references:permission_id"` } func (CUS_Rolehaspermissions) TableName() string { return "RoleHasPermissions" } type FormaterResult struct { model.Roles Permission []model.Permission `json:"permission"` } func GetRoleList(c *gin.Context) serializer.Response { var req GetRoleListRequest c.ShouldBind(&req) var total int64 var list []RoleHasPermissionList page := req.Page pageSize := req.PageSize model.DB. Scopes(util.Paginate(c, page, pageSize)). Preload("Rolehaspermissions.PermissinInfo"). Find(&list).Offset(-1).Limit(-1).Count(&total) // 重新整理輸出格式 var formater_result []FormaterResult for i, v := range list { formater_result = append(formater_result, FormaterResult{Roles: v.