GORM Golang如何优化此代码

I use GORM in my project and I want to create something like DB admin page.

To load records I send GET with params:

category: "name", // database table name

On server I have the next code:

func LoadItems(db *gorm.DB, category string) interface{} {
  var items interface{}

  loadItems := func(i interface{}) {
    err := db.Find(i).Error
    if err != nil {
      panic(err)
    }
    items = i
  }

  switch category {
  case "groups":
    var records []*models.Groups
    loadItems(&records)
  case "departments":
    var records []*models.Departments
    loadItems(&records)
  case .....
    ........
  }

  return items
}

Is it possible to replace switch because I have 10 tables and after record editing I send new data to server, where I re forced to use switch in other function to save it.

I'm not familiar with gorm, but: Maybe store "department" (as key) and a variable of the corresponding model type in a map, and then referencing via key to the model. If not already, the models then have to implement a common interface in order to be able to store them in one map. Though, if that would be a better solution I'm not sure. Maybe a bit easier to maintain, because new model types only have to be added to the map, and you don't have to adjust switches on multiple places in your code.

Another obvious way would be, outsourcing the switch to a function, returning a variable of a common interface type and using that on different places in your code. That would definitely not be faster, but easier to maintain.