XY 問題:我正在嘗試讀取 YAML 檔案,例如下面的檔案,并輸出一組組合了 YAML 檔案中某些鍵和值的元組。例如,鑒于此 YAML 資料:
---
fruit:
apple:
colour:
- green
'banana':
colour:
- yellow
pear:
colour:
- green
- yellow
我想將“水果”下的每個鍵與“顏色”下的每個值組合成元組。我的元組看起來像這樣:
apple:green
banana:yellow
pear:green
pear:yellow
為此,我使用 map[string]interface{} 將我的 YAML 資料解組到 Go - 我不能使用結構體,因為“fruit”下面的鍵的名稱可以是任何東西,所以我需要使用一種動態型別。到目前為止,這是我的代碼:
package main
import (
"fmt"
"log"
"gopkg.in/yaml.v3"
)
var data string = `
---
fruit:
apple:
colour:
- green
'banana':
colour:
- yellow
pear:
colour:
- green
- yellow
`
func main() {
m := make(map[string]interface{})
err := yaml.Unmarshal([]byte(data), &m)
if err != nil {
log.Fatal("Failed to parse YAML file")
}
for _, v := range m {
fruits := v.(map[string]interface{})
for fruit, v2 := range fruits {
colours := v2.(map[string]interface{})["colour"]
for colour := range colours {
fmt.Println("%v:%v\n", fruit, colour)
}
}
}
}
游樂場鏈接:https : //go.dev/play/p/v8iuzmMLtjX
問題是for colour := range colours- 我收到錯誤:
cannot range over colours (type interface {})
我找到了一個類似問題的答案,它說我不能直接將 []interface{} 轉換為 []string,而必須迭代這些值。這就是我在這里嘗試做的。該v2變數適用于一種map[string]interface {}型別,例如可以是map[colour:[green yellow]]. 然后我嘗試將其轉換為另一個 map[string]interface{} 以獲取“color”的值,該值適用于 []interface{} 型別[green yellow]并存盤在colours變數中。
But I can't iterate over colours for some reason. I don't understand what's different about my solution and icza's solution in the linked answer (I've linked their Playground link). The data type of colours in my solution is []interface{}, and in icza's solution the data type of t is also []interface{} - but in that case it is possible to iterate through the slice and access the values within.
Another solution I tried was from this answer to a different question, which was to try directly converting the []interface{} to a []string:
c := colours.([]string)
That also didn't work:
panic: interface conversion: interface {} is []interface {}, not []string
What do I need to do to make this solution work?
uj5u.com熱心網友回復:
由于在編譯時只有映射的鍵是未知的,但結構是已知的,因此您可以比map[string]interface{}以下更具體:
type Document struct {
Fruits map[string]Fruit `yaml:"fruit"`
}
type Fruit struct {
Colours []string `yaml:"colour"`
}
這使得建立你的價值觀變得微不足道:
package main
import (
"fmt"
"gopkg.in/yaml.v3"
)
var data string = `
---
fruit:
apple:
colour:
- green
'banana':
colour:
- yellow
pear:
colour:
- green
- yellow
`
func main() {
var m Document
yaml.Unmarshal([]byte(data), &m)
for name, fruit := range m.Fruits {
for _, colour := range fruit.Colours {
fmt.Printf("%s:%s\n", name, colour)
}
}
}
在操場上試試:https : //go.dev/play/p/phnvRriQmMh
uj5u.com熱心網友回復:
感謝 Cerise Limón 提供的更正幫助我修復了我的解決方案!
package main
import (
"fmt"
"log"
"gopkg.in/yaml.v3"
)
var data string = `
---
fruit:
apple:
colour:
- green
'banana':
colour:
- yellow
pear:
colour:
- green
- yellow
`
func main() {
m := make(map[string]interface{})
err := yaml.Unmarshal([]byte(data), &m)
if err != nil {
log.Fatal("Failed to parse YAML file")
}
for _, v := range m {
fruits := v.(map[string]interface{})
for fruit, v2 := range fruits {
colours := v2.(map[string]interface{})["colour"]
c := colours.([]interface{})
for _, colour := range c {
cs := colour.(string)
fmt.Printf("%v:%v\n", fruit, cs)
}
}
}
}
https://go.dev/play/p/dZT84tDLMjE
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/404466.html
標籤:
