我的 json 数组(conf.json 文件)中有以下内容。
{
"Repos": [
"a",
"b",
"c"
]
}
我正在尝试读取此 json,然后对其进行迭代但卡住了。我对去(和编程)很陌生,所以我很难理解这里发生的事情。
import (
"encoding/json"
"fmt"
"os"
)
type Configuration struct {
Repos []string
}
func read_config() {
file, _ := os.Open("conf.json")
decoder := json.NewDecoder(file)
configuration := Configuration{}
err := decoder.Decode(&configuration)
if err != nil {
fmt.Println("error:", err)
}
fmt.Println(configuration.Repos)
}
到目前为止,这是我所能得到的。这将打印出值,好的[a, b, c]。
我想要做的是能够遍历数组并单独拆分每个值,但在这样做时没有任何运气。我对此采取了错误的方法吗?有一个更好的方法吗?
慕标5832272
ibeautiful
相关分类