将文件内容存入多维字符串变量中。

huangapple go评论115阅读模式
英文:

Getting file content into a multidimensional string var

问题

我正在使用fsnotify包来等待json文件的更改。

我在这段代码中遇到了两个问题。第一个问题是关于ReadFile函数返回的信息。当我打印函数返回的内容时,似乎是空的。

第二个问题是关于fsnotify在第一次读取文件时不起作用,除非我对内容进行一些修改。我也必须从开头读取文件。

  1. type Information struct {
  2. Info []Info `json:"info"`
  3. }
  4. type Info struct {
  5. Type string `json:"type"`
  6. News []New `json:"news"`
  7. }
  8. type New struct {
  9. Name string `json:"name"`
  10. Read bool `json:"read"`
  11. }
  12. func ReadFile(file_name string) *Information {
  13. jsonFile, err := os.Open(file_name)
  14. if err != nil {
  15. fmt.Println(err)
  16. }
  17. fmt.Println("Successfully Opened file_name.json")
  18. defer jsonFile.Close()
  19. byteValue, _ := ioutil.ReadAll(jsonFile)
  20. var infor Information
  21. json.Unmarshal(byteValue, &infor)
  22. return &infor
  23. }
  24. // 主函数
  25. func main() {
  26. // 使用fsnotify读取json文件并等待更改
  27. watcher, err := fsnotify.NewWatcher()
  28. if err != nil {
  29. panic(err)
  30. }
  31. err = watcher.Add(file_json)
  32. if err != nil {
  33. panic(err)
  34. }
  35. for {
  36. select {
  37. case ev, ok := <-watcher.Events:
  38. log.Println("event:", ev)
  39. if !ok {
  40. return
  41. }
  42. if ev.Op&fsnotify.Write == fsnotify.Write {
  43. data := ReadFile(file_name)
  44. fmt.Print("文件信息:\n")
  45. for _, info := range data.Info {
  46. fmt.Printf("信息类型:%s\n", info.Type) // 这里没有打印info.Type的结果
  47. for _, news := range info.News {
  48. fmt.Printf("新闻名称:%s\n", news.Name) // 这里甚至没有打印"新闻名称:"或"新闻已读:"
  49. fmt.Printf("新闻已读:%s\n", strconv.FormatBool(news.Read))
  50. }
  51. }
  52. }
  53. case err := <-watcher.Errors:
  54. log.Println("错误:", err)
  55. }
  56. }
  57. }

这是json文件的内容:

  1. {
  2. "info": [
  3. {
  4. "type": "general",
  5. "news": [
  6. { "name": "abc", "read": true },
  7. { "name": "def", "read": true }
  8. ]
  9. },
  10. {
  11. "type": "confidential",
  12. "news": [
  13. { "name": "xxx", "read": false },
  14. { "name": "yyy", "read": false }
  15. ]
  16. }
  17. ]
  18. }
英文:

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.

  1. type Information struct {
  2. Info []Info `json:&quot;info&quot;`
  3. }
  4. type Info struct {
  5. Type string `json:&quot;type&quot;`
  6. News []New `json:&quot;news&quot;`
  7. }
  8. type New struct {
  9. Name string `json:&quot;name&quot;`
  10. Read bool `json:&quot;read&quot;`
  11. }
  12. func ReadFile(file_name string) *Information {
  13. jsonFile, err := os.Open(file_name)
  14. if err != nil {
  15. fmt.Println(err)
  16. }
  17. fmt.Println(&quot;Successfully Opened file_name.json&quot;)
  18. defer jsonFile.Close()
  19. byteValue, _ := ioutil.ReadAll(jsonFile)
  20. var infor Information
  21. json.Unmarshal(byteValue, &amp;infor)
  22. return &amp;infor
  23. }
  24. // main function
  25. func main() {
  26. // read json file using fsnotify to wait for changes
  27. watcher, err := fsnotify.NewWatcher()
  28. if err != nil {
  29. panic(err)
  30. }
  31. err = watcher.Add(file_json)
  32. if err != nil {
  33. panic(err)
  34. }
  35. for {
  36. select {
  37. case ev, ok := &lt;-watcher.Events:
  38. log.Println(&quot;event:&quot;, ev)
  39. if !ok {
  40. return
  41. }
  42. if ev.Op&amp;fsnotify.Write == fsnotify.Write {
  43. data := ReadFile(file_name)
  44. fmt.Print(&quot;INFORMATION ABOUT FILE:\n&quot;)
  45. for _, info := range data.Info {
  46. fmt.Printf(&quot;Info type: %s\n&quot;, info.Type) // Here is not printing the result of info.Type
  47. for _, news := range info.News {
  48. fmt.Printf(&quot;News Name: %s\n&quot;, news.Name) // Here is not printing even &quot;News Name:&quot; or News Read:&quot;
  49. fmt.Printf(&quot;News Read: %s\n&quot;, strconv.FormatBool(news.Read))
  50. }
  51. }
  52. }
  53. case err := &lt;-watcher.Errors:
  54. log.Println(&quot;error:&quot;, err)
  55. }
  56. }
  57. }

This is the json file:

  1. {
  2. &#160;&#160;&#160; &quot;info&quot;: [
  3. &#160;&#160;&#160;&#160;&#160; {
  4. &#160;&#160;&#160;&#160;&#160;&#160;&#160; &quot;type&quot;: &quot;general&quot;,
  5. &#160;&#160;&#160;&#160;&#160;&#160;&#160; &quot;news&quot;: [
  6. &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; { &quot;name&quot;: &quot;abc&quot;,&#160; &quot;read&quot;: true },
  7. &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; { &quot;name&quot;: &quot;def&quot;,&#160; &quot;read&quot;: true }
  8. &#160;&#160;&#160;&#160;&#160;&#160;&#160; ]
  9. &#160;&#160;&#160;&#160;&#160; },
  10. {
  11. &quot;type&quot;: &quot;confidential&quot;,
  12. &quot;news&quot;: [
  13. { &quot;name&quot;: &quot;xxx&quot;, &quot;read&quot;: false },
  14. { &quot;name&quot;: &quot;yyy&quot;, &quot;read&quot;: false }
  15. ]
  16. },
  17. ]
  18. }

答案1

得分: 1

  1. type Information struct {
  2. Info []Info `json:"info"`
  3. }
  4. type Info struct {
  5. Type string `json:"type"`
  6. News []New `json:"news"` // 这里应该是一个切片定义。
  7. }
  8. type New struct {
  9. Name string `json:"name"`
  10. Read bool `json:"read"`
  11. }
  12. func ReadFile(file_name string) *Information {
  13. jsonFile, err := os.Open(file_name)
  14. if err != nil {
  15. fmt.Println(err)
  16. }
  17. fmt.Println("成功打开文件 file_name.json")
  18. defer jsonFile.Close()
  19. byteValue, _ := ioutil.ReadAll(jsonFile)
  20. var infor Information
  21. json.Unmarshal(byteValue, &infor)
  22. return &infor
  23. }
  1. func main() {
  2. data := ReadFile("./data.json")
  3. for _, news := range data.Info {
  4. for _, v := range news.News {
  5. name := v.Name
  6. // 添加你想要做的操作
  7. fmt.Println(name)
  8. }
  9. }
  10. }

不能得到这样的结果:

  1. getInfo = [general][abc, true, def, true]
  2. [confidential][xxx, false, yyy, false]
英文:
  1. type Information struct {
  2. Info []Info `json:&quot;info&quot;`
  3. }
  4. type Info struct {
  5. Type string `json:&quot;type&quot;`
  6. News []New `json:&quot;news&quot;` // Here should be a slice define.
  7. }
  8. type New struct {
  9. Name string `json:&quot;name&quot;`
  10. Read bool `json:&quot;read&quot;`
  11. }
  12. func ReadFile(file_name string) *Information {
  13. jsonFile, err := os.Open(file_name)
  14. if err != nil {
  15. fmt.Println(err)
  16. }
  17. fmt.Println(&quot;Successfully Opened file_name.json&quot;)
  18. defer jsonFile.Close()
  19. byteValue, _ := ioutil.ReadAll(jsonFile)
  20. var infor Information
  21. json.Unmarshal(byteValue, &amp;infor)
  22. return &amp;infor
  23. }
  1. func main() {
  2. data := ReadFile(&quot;./data.json&quot;)
  3. for _, news := range data.Info {
  4. for _, v := range news.News {
  5. name := v.Name
  6. // Add you want to do
  7. fmt.Println(name)
  8. }
  9. }
  10. }

You can not get like this:

  1. getInfo = [general][abc, true, def, true]
  2. [confidential][xxx, false, yyy, false]

huangapple
  • 本文由 发表于 2022年6月27日 19:03:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/72771038.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定