我想加載json配置文件去lang應用程序。 配置數據是數組,因爲它需要動態設置。加載json數組配置
[ { 「鍵」: 「A」, 「數據」:[1,2,3]}, { 「鍵」: 「B」, 「數據」:[1,2]} , {「key」:「C」,「data」:[1,3]}]
並試圖以這種方式加載。
package main
import (
"flag"
"fmt"
"os"
"encoding/json"
)
type ColInfo struct {
key string `json:"key"`
col []int `json:"data"`
}
type Config struct {
colInfos []ColInfo
}
func main() {
flag.Parse()
file, _ := os.Open("col.conf")
decoder := json.Marshal(file)
configuration := Config{}
if err := decoder.Decode(&configuration); err != nil {
fmt.Println(err)
}
println(configuration.colInfos[0].key)
}
這是錯誤我有
./test2.go:23:多值json.Marshal()的單值上下文
我是什麼這個錯誤?
你告訴我們,你看到的錯誤是什麼?首先看看打開文件時你忽略了一個錯誤 –
在下面添加了錯誤消息。 –
[Go json.Marshal(struct)可能重複返回「{}」](https://stackoverflow.com/questions/26327391/go-json-marshalstruct-returns) –