目錄
問題內容
解決方法
首頁 後端開發 Golang 用於巢狀物件的 Golang 結構

用於巢狀物件的 Golang 結構

Feb 09, 2024 pm 04:39 PM
字串數組

用于嵌套对象的 Golang 结构

php小編蘋果分享了關於Golang語言中用於嵌套物件的結構的詳細介紹。在Golang中,嵌套物件的結構是一種強大的特性,它允許我們在一個結構體中嵌套其他結構體或介面類型。透過嵌套物件的結構,我們可以輕鬆地組合和重複使用程式碼,提高程式碼的可讀性和可維護性。不僅如此,嵌套物件的結構還可以實現多重繼承的效果,讓我們能夠更靈活地設計和建構複雜的資料結構。在本文中,我們將詳細探討Golang中嵌套物件的結構的使用方法和技巧,幫助讀者更好地理解和應用這項特性。

問題內容

我目前正在使用 gofiber v2 建立 golang api。

我在 mongo 資料庫中擁有以下音樂曲目的文檔結構:

{
    "_id" : objectid("63cc26cb86ae1611380e1206"),
    "active" : 1,
    "exclusive" : "false",
    "track_title" : "burn slow (sting)",
    "artist_id" : "395",
    "artist_name" : "david hollandsworth",
    "album_title" : "cult justice 23",
    "composer" : "david hollandsworth",
    "duration" : "00:16",
    "publisher" : "fliktrax, llc",
    "description" : "t.v. drama, rural tension, apprehension. style: \"hell on wheels\" soundtrack.",
    "url_path" : "davidhollandsworth/cultjustice23/burn-slow-sting.wav",
    "vocal_type" : "instrumental",
    "beats_per_minute" : "80",
    "file_path_compressed" : "davidhollandsworth/cultjustice23/burn-slow-sting.mp3",
    "file_path_uncompressed" : "davidhollandsworth/cultjustice23/burn-slow-sting.wav",

    "genres" : [ 
        "tension", 
        "americana", 
        "tv drama"
    ],
    "genres_keys" : [ 
        "tension", 
        "americana", 
        "tv drama"
    ],
    "moods" : [ 
        "tension", 
        "bluesy", 
        "spacey"
    ],
    "styles" : [ 
        "tv drama", 
        "unsolved mystery", 
        "western"
    ],
    "instruments" : [ 
        "dobro", 
        "banjo", 
        "percussion"
    ],
    "keywords" : [ 
        "rural-tension", 
        " showdown", 
        " apprehension", 
        " uncertainty", 
        " light-tension", 
        " strings-tension", 
        " heartland", 
        " trouble", 
        " uneasy", 
        " cautious", 
        " outlaw", 
        " yellowstone", 
        " bayou", 
        " gritty", 
        " swampy", 
        " swamp-people", 
        " southern", 
        " uncertain", 
        " drama", 
        " apprehension", 
        " bluesy", 
        " blues", 
        " shack", 
        " poor-folk", 
        " primitive"
    ],
    "sounds_like" : [ 
        "brian tyler", 
        "max richter", 
        "t.v. drama"
    ],
    "resembles_song" : [ 
        "hell on wheels", 
        "yellowstone", 
        "rural/outlaw/tension"
    ],
    "last_modified" : 1674323659,
    "variation_count" : 5,
    "variations" : {
        "_id" : objectid("63cc26bc86ae1611380e1200"),
        "track_title" : "burn slow",
        "artist_name" : "david hollandsworth",
        "master_track_id" : "63cc26bc86ae1611380e1200",
        "master_track" : objectid("63cc26bc86ae1611380e1200"),
        "merged" : 1,
        "variation_count" : 5,
        "variations" : {
            "63cc26bc86ae1611380e1200" : {
                "track_id" : "63cc26bc86ae1611380e1200",
                "track_title" : "burn slow",
                "artist_name" : "david hollandsworth"
            },
            "63cc26c086ae1611380e1203" : {
                "track_id" : "63cc26c086ae1611380e1203",
                "track_title" : "burn slow (bed mix)",
                "artist_name" : "david hollandsworth"
            },
            "63cc26c386ae1611380e1204" : {
                "track_id" : "63cc26c386ae1611380e1204",
                "track_title" : "burn slow (cutdown)",
                "artist_name" : "david hollandsworth"
            },
            "63cc26c786ae1611380e1205" : {
                "track_id" : "63cc26c786ae1611380e1205",
                "track_title" : "burn slow (lows and perc)",
                "artist_name" : "david hollandsworth"
            },
            "63cc26cb86ae1611380e1206" : {
                "track_id" : "63cc26cb86ae1611380e1206",
                "track_title" : "burn slow (sting)",
                "artist_name" : "david hollandsworth"
            }
        }
    }
}
登入後複製

目前,我的 golang 模型中有以下軌道結構:

type Track struct {
    ID                   primitive.ObjectID `bson:"_id, omitempty" json:"_id"`
    TrackTitle           string             `bson:"track_title" json:"track_title"`
    ArtistId             string             `bson:"artist_id" json:"artist_id"`
    ArtistName           string             `bson:"artist_name" json:"artist_name"`
    AlbumTitle           string             `bson:"album_title" json:"album_title"`
    Composer             string             `bson:"composer" json:"composer"`
    Publisher            string             `bson:"publisher" json:"publisher"`
    Description          string             `bson:"description" json:"description"`
    Duration             string             `bson:"duration" json:"duration"`
    UrlPath              string             `bson:"url_path" json:"url_path"`
    VocalType            string             `bson:"vocal_type" json:"vocal_type"`
    BeatsPerMinute       string             `bson:"beats_per_minute" json:"beats_per_minute"`
    FilePathCompressed   string             `bson:"file_path_compressed" json:"bfile_path_compressed"`
    FilePathUncompressed string             `bson:"file_path_uncompressed" json:"file_path_uncompressed"`
    PreviewURL           string             `bson:"preview_url" json:"preview_url"`
    Genres               []interface{}      `bson:"genres" json:"genres"`
    GenresKeys           []interface{}      `bson:"genres_keys" json:"genres_keys"`
    Moods                []interface{}      `bson:"moods" json:"moods"`
    Styles               []interface{}      `bson:"styles" json:"styles"`
    Instruments          []interface{}      `bson:"instruments" json:"instruments"`
    Keywords             []interface{}      `bson:"keywords" json:"keywords"`
    SoundsLike           []interface{}      `bson:"sounds_like" json:"sounds_like"`
    ResemblesSong        []interface{}      `bson:"resembles_song" json:"resembles_song"`
    LastModified         int                `bson:"last_modified" json:"last_modified"`
    VariationCount       int                `bson:"variation_count" json:"variation_count"`
}
登入後複製

目前,所有文件欄位都已正確解碼為 json,但是我現在在如何建構嵌入的「variations.variations」欄位(請注意,變體中存在變體)方面陷入了僵局。嵌入變體的結構是一個沒有鍵的對象,而是一個動態的 mongo id 字串。

我嘗試實作自訂結構和介面類型,但沒有成功。

如果有人以前遇到過此問題,我們將不勝感激。

解決方法

我建議盡可能避免使用 interface{} (或 any),使用具體類型。例如。 genres 是一個字串數組,在 go 中使用 []string

對於 variations.variations 字段,您可以使用 map 以及 string 鍵和描述其元素的結構類型。

type Track struct {
    ID                   primitive.ObjectID `bson:"_id, omitempty" json:"_id"`
    TrackTitle           string             `bson:"track_title" json:"track_title"`
    ArtistId             string             `bson:"artist_id" json:"artist_id"`
    ArtistName           string             `bson:"artist_name" json:"artist_name"`
    AlbumTitle           string             `bson:"album_title" json:"album_title"`
    Composer             string             `bson:"composer" json:"composer"`
    Publisher            string             `bson:"publisher" json:"publisher"`
    Description          string             `bson:"description" json:"description"`
    Duration             string             `bson:"duration" json:"duration"`
    UrlPath              string             `bson:"url_path" json:"url_path"`
    VocalType            string             `bson:"vocal_type" json:"vocal_type"`
    BeatsPerMinute       string             `bson:"beats_per_minute" json:"beats_per_minute"`
    FilePathCompressed   string             `bson:"file_path_compressed" json:"bfile_path_compressed"`
    FilePathUncompressed string             `bson:"file_path_uncompressed" json:"file_path_uncompressed"`
    PreviewURL           string             `bson:"preview_url" json:"preview_url"`
    Genres               []string           `bson:"genres" json:"genres"`
    GenresKeys           []string           `bson:"genres_keys" json:"genres_keys"`
    Moods                []string           `bson:"moods" json:"moods"`
    Styles               []string           `bson:"styles" json:"styles"`
    Instruments          []string           `bson:"instruments" json:"instruments"`
    Keywords             []string           `bson:"keywords" json:"keywords"`
    SoundsLike           []string           `bson:"sounds_like" json:"sounds_like"`
    ResemblesSong        []string           `bson:"resembles_song" json:"resembles_song"`
    LastModified         int                `bson:"last_modified" json:"last_modified"`
    VariationCount       int                `bson:"variation_count" json:"variation_count"`
    Variations           Variations         `bson:"variations" json:"variations"`
}

type Variations struct {
    ID             primitive.ObjectID   `bson:"_id" json:"_id"`
    TrackTitle     string               `bson:"track_title" json:"track_title"`
    ArtistName     string               `bson:"artist_name" json:"artist_name"`
    MasterTrackID  string               `bson:"master_track_id" json:"master_track_id"`
    MasterTrack    primitive.ObjectID   `bson:"master_track" json:"master_track"`
    Merged         int                  `bson:"merged" json:"merged"`
    VariationCount int                  `bson:"variation_count" json:"variation_count"`
    Variations     map[string]Variation `bson:"variations" json:"variations"`
}

type Variation struct {
    TrackID    string `bson:"track_id" json:"track_id"`
    TrackTitle string `bson:"track_title" json:"track_title"`
    ArtistName string `bson:"artist_name" json:"artist_name"`
}
登入後複製

以上是用於巢狀物件的 Golang 結構的詳細內容。更多資訊請關注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脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

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

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

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

oracle中split()函數用法 oracle中split()函數用法 May 07, 2024 pm 01:06 PM

SPLIT() 函數透過指定的分隔符號拆分字串為數組,傳回字串數組,其中每個元素都是原始字串中以分隔符號分隔的部分。用法包括:將逗號分隔的值清單拆分為陣列、從路徑中提取檔案名稱、將電子郵件地址拆分為使用者名稱和網域。

java怎麼對字串排序 java怎麼對字串排序 Apr 02, 2024 am 02:18 AM

Java 中對字串排序的方法:使用 Arrays.sort() 方法對字串陣列按升序排序。使用 Collections.sort() 方法對字串清單按升序排序。使用 Comparator 介面對字串進行自訂排序。

\0在c語言中是什麼意思 \0在c語言中是什麼意思 Apr 27, 2024 pm 10:54 PM

C 語言中,\0 是字串的結束標誌,稱為空字元或終止符。由於字串在記憶體中以位元組數組形式存儲,編譯器透過 \0 識別字串結束,確保正確處理字串。 \0 工作原理:編譯器遇到 \0 時停止讀取字符,之後的字符被忽略。 \0 自身不佔儲存空間。好處包括可靠的字串處理、提高效率(無需掃描整個陣列查找結束)以及方便比較和操作。

args在java中是什麼意思 args在java中是什麼意思 Apr 25, 2024 pm 10:15 PM

args 在 Java 中表示命令列參數,是一個字串數組,包含程式啟動時傳遞給它的參數列表。它僅在 main 方法中可用,其預設值為一個空數組,透過索引可以存取每個參數。 args 用於接收和處理命令列參數,從而在程式啟動時進行配置或提供輸入資料。

java中的args是什麼意思 java中的args是什麼意思 May 07, 2024 am 02:24 AM

args 是 Java 中 main 方法的特殊參數數組,用於取得命令列參數或外部輸入的字串數組。透過存取 args 數組,程式可以讀取這些參數,並根據需要進行處理。

PHP 函數中人工智慧技術的應用 PHP 函數中人工智慧技術的應用 May 01, 2024 pm 01:15 PM

AI技術已與PHP函數結合,增強了應用程式的功能。具體的AI應用包括:使用機器學習演算法對文本進行分類,如樸素貝葉斯。使用自然語言處理技術進行深入文本分析,如分詞和詞幹提取。

在C語言環境下如何對中文字元進行排序? 在C語言環境下如何對中文字元進行排序? Feb 18, 2024 pm 02:10 PM

如何在C語言程式設計軟體中實現中文字元排序功能?在現代社會,中文字元排序功能在許多軟體中都是不可或缺的功能之一。無論是在文字處理軟體、搜尋引擎或資料庫系統中,都需要對中文字元進行排序,以便更好地展示和處理中文文字資料。而在C語言程式設計中,如何實現中文字元排序功能呢?下面將簡要介紹一種方法。首先,為了在C語言中實作中文字元排序功能,我們需要使用到字串比較函數。然

C++ 函式對程式效能有哪些影響? C++ 函式對程式效能有哪些影響? Apr 12, 2024 am 09:39 AM

函数对C++程序性能的影响包括函数调用开销、局部变量和对象分配开销:函数调用开销:包括堆栈帧分配、参数传递和控制权转移,对小函数影响显著。局部变量和对象分配开销:大量局部变量或对象创建和销毁会导致堆栈溢出和性能下降。

See all articles