目錄
問題內容
解決方法
首頁 後端開發 Golang 如何從 Go 中的 for 迴圈傳回介面清單?

如何從 Go 中的 for 迴圈傳回介面清單?

Feb 09, 2024 pm 12:10 PM
go語言

如何从 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中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

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

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

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

熱門話題

Java教學
1663
14
CakePHP 教程
1420
52
Laravel 教程
1313
25
PHP教程
1266
29
C# 教程
1239
24
在Go語言中使用Redis Stream實現消息隊列時,如何解決user_id類型轉換問題? 在Go語言中使用Redis Stream實現消息隊列時,如何解決user_id類型轉換問題? Apr 02, 2025 pm 04:54 PM

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

GoLand中自定義結構體標籤不顯示怎麼辦? GoLand中自定義結構體標籤不顯示怎麼辦? Apr 02, 2025 pm 05:09 PM

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

Go的爬蟲Colly中Queue線程的問題是什麼? Go的爬蟲Colly中Queue線程的問題是什麼? Apr 02, 2025 pm 02:09 PM

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

在 Go 語言中,為什麼使用 Println 和 string() 函數打印字符串會出現不同的效果? 在 Go 語言中,為什麼使用 Println 和 string() 函數打印字符串會出現不同的效果? Apr 02, 2025 pm 02:03 PM

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

Go語言中用於浮點數運算的庫有哪些? Go語言中用於浮點數運算的庫有哪些? Apr 02, 2025 pm 02:06 PM

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

Go語言中`var`和`type`關鍵字定義結構體的區別是什麼? Go語言中`var`和`type`關鍵字定義結構體的區別是什麼? Apr 02, 2025 pm 12:57 PM

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

Go語言中哪些庫是由大公司開發或知名的開源項目提供的? Go語言中哪些庫是由大公司開發或知名的開源項目提供的? Apr 02, 2025 pm 04:12 PM

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

使用 sql.Open 時,DSN 傳空為什麼不報錯? 使用 sql.Open 時,DSN 傳空為什麼不報錯? Apr 02, 2025 pm 12:54 PM

使用sql.Open時,DSN傳空為什麼不報錯?在Go語言中,sql.Open...

See all articles