在 go 中獲取兩個地圖的並集 - Getting the union of two maps in go

問題:

I have a recursive function that creates objects representing file paths (the keys are paths and the values are info about the file).我有一個遞歸函數,它創建表示文件路徑的對象(鍵是路徑,值是關於文件的信息)。 It's recursive as it's only meant to handle files, so if a directory is encountered, the function is recursively called on the directory.它是遞歸的,因爲它僅用於處理文件,因此如果遇到目錄,則會在該目錄上遞歸調用該函數。

All that being said, I'd like to do the equivalent of a set union on two maps (ie the "main" map updated with the values from the recursive call).話雖如此,我想在兩個地圖(即“主”地圖更新了來自遞歸調用的值)上進行等價的集合並集。 Is there an idiomatic way to do this aside from iterating over one map and assigning each key, value in it to the same thing in the other map?除了迭代一個地圖並將其中的每個鍵和值分配給另一個地圖中的相同內容之外,是否有一種慣用的方法來做到這一點?

That is: given a,b are of type map [string] *SomeObject , and a and b are eventually populated, is there any way to update a with all the values in b ?即:給a,b的類型的map [string] *SomeObject ,以及ab最終被填充,有沒有什麼辦法來更新a與所有的值b


解決方案:

參考: https://stackoom.com/en/question/1Wux0
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章