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

Golang適合快速開發和並發編程,而C 更適合需要極致性能和底層控制的項目。 1)Golang的並發模型通過goroutine和channel簡化並發編程。 2)C 的模板編程提供泛型代碼和性能優化。 3)Golang的垃圾回收方便但可能影響性能,C 的內存管理複雜但控制精細。

goimpactsdevelopmentpositationality throughspeed,效率和模擬性。 1)速度:gocompilesquicklyandrunseff,IdealforlargeProjects.2)效率:效率:ITScomprehenSevestAndardArdardArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdEcceSteral Depentencies,增強的Depleflovelmentimency.3)簡單性。

C 更適合需要直接控制硬件資源和高性能優化的場景,而Golang更適合需要快速開發和高並發處理的場景。 1.C 的優勢在於其接近硬件的特性和高度的優化能力,適合遊戲開發等高性能需求。 2.Golang的優勢在於其簡潔的語法和天然的並發支持,適合高並發服務開發。

Golang在实际应用中表现出色,以简洁、高效和并发性著称。1)通过Goroutines和Channels实现并发编程,2)利用接口和多态编写灵活代码,3)使用net/http包简化网络编程,4)构建高效并发爬虫,5)通过工具和最佳实践进行调试和优化。

Go語言的核心特性包括垃圾回收、靜態鏈接和並發支持。 1.Go語言的並發模型通過goroutine和channel實現高效並發編程。 2.接口和多態性通過實現接口方法,使得不同類型可以統一處理。 3.基本用法展示了函數定義和調用的高效性。 4.高級用法中,切片提供了動態調整大小的強大功能。 5.常見錯誤如競態條件可以通過gotest-race檢測並解決。 6.性能優化通過sync.Pool重用對象,減少垃圾回收壓力。

Go語言在構建高效且可擴展的系統中表現出色,其優勢包括:1.高性能:編譯成機器碼,運行速度快;2.並發編程:通過goroutines和channels簡化多任務處理;3.簡潔性:語法簡潔,降低學習和維護成本;4.跨平台:支持跨平台編譯,方便部署。

關於SQL查詢結果排序的疑惑學習SQL的過程中,常常會遇到一些令人困惑的問題。最近,筆者在閱讀《MICK-SQL基礎�...


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

Atom編輯器mac版下載
最受歡迎的的開源編輯器

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。