如何读取多个json文件

如何读取多个json文件,json,go,Json,Go,我正在尝试从多个json文件中读取json数据。我不确定如何读取每个文件并连接所有结果 json文件名为test1.json、test2.json、test3.json..等等,具有相同的数据结构,但我在读取所有文件时遇到了一个问题,我的代码似乎只显示最后一个文件。我根据文件名连接了一个字符串,但似乎对我不起作用 type Book struct { Id string `json: "id"` Title string `json: "title"` } func ma

我正在尝试从多个json文件中读取json数据。我不确定如何读取每个文件并连接所有结果

json文件名为test1.json、test2.json、test3.json..等等,具有相同的数据结构,但我在读取所有文件时遇到了一个问题,我的代码似乎只显示最后一个文件。我根据文件名连接了一个字符串,但似乎对我不起作用

type Book struct {
    Id    string `json: "id"`
    Title string `json: "title"`
}

func main() {
    fileIndex := 2 // three json files. All named test1.json, test2.json and test3.json

    var master []Book

    for i := 0; i <= fileIndex; i++ {
        fileName := fmt.Sprintf("%s%d%s", "test", fileIndex, ".json")

        // Open jsonFile
        jsonFile, err := os.Open(fileName)

        defer jsonFile.Close()

        byteValue, _ := ioutil.ReadAll(jsonFile)
        fmt.Println(byteValue)
        var book []Book

        json.Unmarshal(byteValue, &book)
        fmt.Println(book) // all print shows the test3.json result 
    }
}
type Book结构{
Id字符串`json:“Id”`
标题字符串`json:“标题”`
}
func main(){
fileIndex:=2//三个json文件。所有文件都命名为test1.json、test2.json和test3.json
var master[]手册

对于i:=0;i您在生成文件名时使用的是
fileIndex
,而不是在for循环中使用
i
。 更改后的代码为:

type Book结构{
Id字符串`json:“Id”`
标题字符串`json:“标题”`
}
func main(){
fileIndex:=2//三个json文件。所有文件都命名为test1.json、test2.json和test3.json
var master[]手册

对于i:=0;我最好连接未经格式化的json结果?