如何從 Go 中的 for 迴圈傳回介面清單?
在Go語言中,我們可以透過for迴圈來遍歷陣列、切片、映射等資料結構。但是,有時候我們需要在循環中回傳一個介面清單。這是一個常見的需求,因為介面是一種抽象類型,可以代表多種具體類型。本文將介紹如何在Go中使用for迴圈返回介面清單。首先,我們需要理解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,我必須取得一個interface{}列表,我嘗試重寫如下程式碼,但我不知道下一步該怎麼寫,如何從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 }
解決方法
如果您希望每個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"` } // 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 }
如果所有型別總共有 10 個結果:
i write it if you need it
以上是如何從 Go 中的 for 迴圈傳回介面清單?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Go語言中使用RedisStream實現消息隊列時類型轉換問題在使用Go語言與Redis...

GoLand中自定義結構體標籤不顯示怎麼辦?在使用GoLand進行Go語言開發時,很多開發者會遇到自定義結構體標籤在�...

Go爬蟲Colly中的Queue線程問題探討在使用Go語言的Colly爬蟲庫時,開發者常常會遇到關於線程和請求隊列的問題。 �...

Go語言中字符串打印的區別:使用Println與string()函數的效果差異在Go...

Go語言中用於浮點數運算的庫介紹在Go語言(也稱為Golang)中,進行浮點數的加減乘除運算時,如何確保精度是�...

Go語言中結構體定義的兩種方式:var與type關鍵字的差異Go語言在定義結構體時,經常會看到兩種不同的寫法:一�...

Go語言中哪些庫是大公司開發或知名開源項目?在使用Go語言進行編程時,開發者常常會遇到一些常見的需求,�...
