我想创建一个接受字符串参数的函数,并根据字符串返回不同的类型
在我当前的项目中,我使用 Go 版本 1.11.4 作为 Rest API,使用 PostgreSQL 11 来存储数据。该函数是为了避免代码冗余,我只需要一个函数来处理请求,没有这个函数我会写更多的请求处理程序。
功能是:
func GetArrayOfModelStructs(table_name string) interface{} {
if table_name == "prefs_n_cities" {
Prefs_n_cities_array := &models.Prefs_n_cities_array{}
return Prefs_n_cities_array
} else if table_name == "position_owner" {
Position_owners := &models.Position_owners{}
return Position_owners
} else if table_name == "region" {
Regions := &models.Regions{}
return Regions
} else if table_name == "district" {
Districts := &models.Districts{}
return Districts
} else if table_name == "locality" {
Localities := &models.Localities{}
return Localities
} else if table_name == "locality_type" {
Locality_types := &models.Locality_types{}
return Locality_types
} else if table_name == "population" {
Populations := &models.Populations{}
return Populations
}
return nil
}
func GetModelStructs(table_name string) interface{} {
if table_name == "prefs_n_cities" {
return models.Prefs_n_cities{}
} else if table_name == "position_owner" {
return models.Position_owner{}
} else if table_name == "region" {
return models.Regions{}
} else if table_name == "district" {
return models.District{}
} else if table_name == "locality" {
return models.Locality{}
} else if table_name == "locality_type" {
return models.Locality_type{}
} else if table_name == "population" {
return models.Population{}
}
return nil
}
我的结构示例如下:
type Prefs_n_cities struct {
id int32
Name string
Description string
Region_id int32
}
type Prefs_n_cities_array struct {
Array []Prefs_n_cities
}
动漫人物
相关分类