I hope someone could help me with this issue because I have been scratching my head for a while.
I have a project where I am trying to load json into a struct in go. I have followed exactly several tutorials online, but keep getting no data back and no error.
My json file is called page_data.json and looks like:
[
{
"page_title": "Page1",
"page_description": "Introduction",
"link": "example_link",
"authors":
[
"Author1",
"Author2",
"Author3",
]
},
// second object, same as the first
]
But when I try the following in go:
package main
import (
"fmt"
"encoding/json"
"os"
"io/ioutil"
)
type PageData struct {
Title string `json: "page_title"`
Description string `json: "page_description"`
Link string `json: "link"`
Authors []string `json: "authors"`
}
func main() {
var numPages int = LoadPageData("page_data.json")
fmt.Printf("Num Pages: %d", numPages)
}
func LoadPageData(path string) int {
jsonFile, err := os.Open(path)
if err != nil {
fmt.Println(err)
}
defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
var pageList []PageData
json.Unmarshal(byteValue, &pageList)
return len(pageList)
}
the output I get is:
Num Pages: 0