Getting file content into a multidimensional string var

Viewed 63

I'm using the fsnotify packet to wait for changes in a json file.

I have two problems with this code. The first one is regarding the info returned by ReadFile function. Looks like when I print something returned by the function is empty.

Second issue is regarding the fsnotify that is not reading the file the first time unless i do some modification on the content. I must read the file from the beggining as well.

type Information struct {
    Info []Info `json:"info"`
}

type Info struct {
    Type string `json:"type"`
    News []New  `json:"news"`
}

type New struct {
    Name string `json:"name"`
    Read bool   `json:"read"`
}

func ReadFile(file_name string) *Information {
    jsonFile, err := os.Open(file_name)
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println("Successfully Opened file_name.json")

    defer jsonFile.Close()

    byteValue, _ := ioutil.ReadAll(jsonFile)

    var infor Information

    json.Unmarshal(byteValue, &infor)

    return &infor
}

// main function
func main() {
   // read json file using fsnotify to wait for changes
    watcher, err := fsnotify.NewWatcher()

    if err != nil {
        panic(err)
    }

    err = watcher.Add(file_json)
    if err != nil {
        panic(err)
    }

    for {
        select {
        case ev, ok := <-watcher.Events:
            log.Println("event:", ev)
            if !ok {
                return
            }
            if ev.Op&fsnotify.Write == fsnotify.Write {

                data := ReadFile(file_name)
                fmt.Print("INFORMATION ABOUT FILE:\n") 

                for _, info := range data.Info {
                    fmt.Printf("Info type: %s\n", info.Type) // Here is not printing the result of info.Type
                    for _, news := range info.News {
                        fmt.Printf("News Name: %s\n", news.Name) // Here is not printing even "News Name:" or News Read:"
                        fmt.Printf("News Read: %s\n", strconv.FormatBool(news.Read))
                    }
                }
            }
        case err := <-watcher.Errors:
            log.Println("error:", err)
        }
    }
}

This is the json file:

{
        "info": [
          {
            "type": "general",
            "news": [
              { "name": "abc",  "read": true },
              { "name": "def",  "read": true }
            ]
          },
          {
            "type": "confidential",
            "news": [
              { "name": "xxx",  "read": false },
              { "name": "yyy",  "read": false }
            ]
          },

        ]
}
1 Answers
type Information struct {
    Info []Info `json:"info"`
}

type Info struct {
    Type string `json:"type"`
    News []New  `json:"news"` // Here should be a slice define.
}

type New struct {
    Name string `json:"name"`
    Read bool   `json:"read"`
}

func ReadFile(file_name string) *Information {
    jsonFile, err := os.Open(file_name)
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println("Successfully Opened file_name.json")

    defer jsonFile.Close()

    byteValue, _ := ioutil.ReadAll(jsonFile)

    var infor Information

    json.Unmarshal(byteValue, &infor)

    return &infor
}
func main() {
    data := ReadFile("./data.json")
    for _, news := range data.Info {
        for _, v := range news.News {
            name := v.Name
            // Add you want to do
            fmt.Println(name)
        }
    }
}

You can not get like this:

getInfo = [general][abc, true, def, true]
          [confidential][xxx, false, yyy, false]
Related