英文:
How to return interface list from a for loop in Go?
问题
如何从Go的for循环中返回接口列表?
假设我有以下数据:
id | name | project_type |
---|---|---|
1 | project_name | 1 |
2 | project_name | 1 |
3 | project_name | 1 |
4 | project_name | 2 |
5 | project_name | 2 |
6 | project_name | 3 |
7 | project_name | 3 |
8 | project_name | 3 |
9 | project_name | 4 |
10 | project_name | 4 |
我可以使用以下Go代码获取project_type=1和project_type=2的两个列表:
func (d *db) ProjectList(Type, uid string, size uint64) (interface{}, interface{}, error) {
type resp struct {
Name string `json:"name"`
Id string `json:"id"`
ProjectType string `json:"project_type"`
}
var project_type_1 []*resp
var project_type_2 []*resp
sql = fmt.Sprintf(sql, where.String())
_, err := d.ctx.DB().SelectBySql("select * from project where project_type=1 order by rand() limit 10").Load(&project_type_1)
_, err = d.ctx.DB().SelectBySql("select * from project where project_type=2 order by rand() limit 10").Load(&project_type_2)
return project_type_1, project_type_2, err
}
但是现在project_type的数据是一个JSON,格式为[{"project_type":1,"name":"something else"},{"project_type":2,"name":"something else"},{"project_type":3,"name":"something else"},{"project_type":4,"name":"something else"}]
,project_type的值超过2个,我需要获取一个接口{}列表。我尝试将代码重写如下,但是我不知道接下来该如何编写,如何从Go的for循环中返回接口列表?非常感谢任何建议。
func NewProjectList(d *db) ([]interface{}, error) {
var s = make([]ProjectType, 0)
data, err := d.QueryString("project_type")
if err != nil {
return nil, err
}
err = json.Unmarshal([]byte(data), &s)
if err != nil {
return nil, err
}
for _, shortType := range s {
fmt.Println("this is shortType", shortType)
}
return nil, err
}
英文:
How to return interface list from a for loop in Go?
Supposed I have data as below:
id | name | project_type |
---|---|---|
1 | project_name | 1 |
2 | project_name | 1 |
3 | project_name | 1 |
4 | project_name | 2 |
5 | project_name | 2 |
6 | project_name | 3 |
7 | project_name | 3 |
8 | project_name | 3 |
9 | project_name | 4 |
10 | project_name | 4 |
I can use the below go code to get two list of project_type=1 and project_type=2,
func (d *db) ProjectList(Type, uid string, size uint64) (interface{}, interface{}, error) {
type resp struct {
Name string `json:"name"`
Id string `json:"id"`
ProjectType string `json:"project_type"`
}
var project_type_1 []*resp
var project_type_2 []*resp
sql = fmt.Sprintf(sql, where.String())
_, err := d.ctx.DB().SelectBySql("select * from project where project_type=1 order by rand() limit 10").Load(&project_type_1)
_, err = d.ctx.DB().SelectBySql("select * from project where project_type=2 order by rand() limit 10").Load(&project_type_2)
return project_type_1, project_type_2, err
}
But now the data of project_type is a JSON of [{"project_type":1,"name":"something else"},{"project_type":2,"name":"something else"},{"project_type":3,"name":"something else"},{"project_type":4,"name":"something else"}]
, project_type is more than 2, I have to get a interface{} list, I tried to re-write code as below, But I don't know how do write next, How to return interface list from a for loop in Go?, thanks so much for any advice.
func NewProjectList(d *db) ([]interface{}, error) {
var s = make([]ProjectType, 0)
data, err := d.QueryString("project_type")
if err != nil {
return nil, err
}
err = json.Unmarshal([]byte(data), &s)
if err != nil {
return nil, err
}
for _, shortType := range s {
fmt.Println("this is shortType", shortType)
}
return nil, err
}
答案1
得分: 1
如果你想让每个project_type都有10个结果(总共10*n个结果):
func (d *db) ProjectList(Type, uid string, size uint64) ([]interface{}, error) {
type resp struct {
Name string `json:"name"`
Id string `json:"id"`
ProjectType string `json:"project_type"`
}
// 获取所有类型,你可以从数据库中获取
// 例如:select DISTINCT ProjectType from project
types := []string{"1", "2", "3", "4"}
ans := []interface{}{}
for _, sType := range types {
var project_type []*resp
sql := fmt.Sprintf("select * from project where project_type=%s order by rand() limit 10", sType)
_, err := d.ctx.DB().SelectBySql(sql).Load(&project_type)
ans = append(ans, project_type)
}
return ans, nil
}
如果所有类型总共只有10个结果:
// 如果需要,我可以为你编写这部分代码
英文:
if you want each project_type have 10 results(all 10*n results):
func (d *db) ProjectList(Type, uid string, size uint64) ([]interface{}, error) {
type resp struct {
Name string `json:"name"`
Id string `json:"id"`
ProjectType string `json:"project_type"`
}
// get all types, you can get it from db
// example: select DISTINCT ProjectType from project
types := []string{"1", "2", "3", "4"}
ans := []interface{}{}
for _, sType := range types {
var project_type []*resp
sql = fmt.Sprintf(sql, where.String())
_, err := d.ctx.DB().SelectBySql("select * from project where project_type=" + sType + " order by rand() limit 10").Load(&project_type)
ans = append(ans, project_type)
}
return ans, nil
}
if A total of 10 results for all types:
i write it if you need it
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论