尝试在Go语言中使用通道,但数据未正确发送/接收到通道中。

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

Trying to use channels in go but data is not properly sent/received into the channel

问题

希望能够快速解析非常多相似的URL(只有一个'id'元素在每个URL中不同),并将响应体转储到一个通道中,稍后主函数将查询该通道并用于构建文本文件。

getpageCanal()函数内部,响应体似乎没问题,但之后,我不明白为什么通道没有正确加载响应体字符串。

  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "net/http"
  6. "os"
  7. )
  8. func main() {
  9. initial := "https://www1.medion.de/downloads/index.pl?op=detail&id="
  10. ending := "&type=treiber&lang=uk"
  11. links := []string{}
  12. os.Remove("dump.txt")
  13. dumpFile, _ := os.Create("dump.txt")
  14. c := make(chan string)
  15. for i := 16000; i < 16004; i++ {
  16. links = append(links, initial+fmt.Sprint(i)+ending)
  17. }
  18. fmt.Println(links[0])
  19. for _, link := range links {
  20. //希望将此处变为goroutine,但首先我需要让它正常工作
  21. getpageCanal(c, link)
  22. }
  23. for el := range c {
  24. fmt.Println(el)
  25. n, err := dumpFile.WriteString(el)
  26. if err != nil {
  27. fmt.Println(err)
  28. }
  29. if n == 0 {
  30. fmt.Println("主函数中没有写入任何内容")
  31. }
  32. }
  33. }
  34. func getpageCanal(canal chan string, url string) {
  35. defer close(canal)
  36. page, err := http.Get(url)
  37. if err != nil {
  38. fmt.Println("你搞砸了,小伙子")
  39. }
  40. content, er2 := ioutil.ReadAll(page.Body)
  41. //fmt.Println(content)
  42. if er2 != nil {
  43. fmt.Println(er2)
  44. }
  45. canal <- string(content)
  46. }
  47. <details>
  48. <summary>英文:</summary>
  49. The hope is to quickly parse a very large number of similar URLs (only one &#39;id&#39; element differs from one to the next) and dump the response body into a channel that will later be queried by the main function and used to build a text file.
  50. Inside the `getpageCanal()` function, the body seems to be ok, but after that, I don&#39;t understand why the channel doesn&#39;t properly load the body string.
  51. ```golang
  52. package main
  53. import (
  54. &quot;fmt&quot;
  55. &quot;io/ioutil&quot;
  56. &quot;net/http&quot;
  57. &quot;os&quot;
  58. )
  59. func main() {
  60. initial := &quot;https://www1.medion.de/downloads/index.pl?op=detail&amp;id=&quot;
  61. ending := &quot;&amp;type=treiber&amp;lang=uk&quot;
  62. links := []string{}
  63. os.Remove(&quot;dump.txt&quot;)
  64. dumpFile, _ := os.Create(&quot;dump.txt&quot;)
  65. c := make(chan string)
  66. for i := 16000; i &lt; 16004; i++ {
  67. links = append(links, initial+fmt.Sprint(i)+ending)
  68. }
  69. fmt.Println(links[0])
  70. for _, link := range links {
  71. //the hope is to make this a go routine, but first I need to just make it work
  72. getpageCanal(c, link)
  73. }
  74. for el := range c {
  75. fmt.Println(el)
  76. n, err := dumpFile.WriteString(el)
  77. if err != nil {
  78. fmt.Println(err)
  79. }
  80. if n == 0 {
  81. fmt.Println(&quot; nothing written in main&quot;)
  82. }
  83. }
  84. }
  85. func getpageCanal(canal chan string, url string) {
  86. defer close(canal)
  87. page, err := http.Get(url)
  88. if err != nil {
  89. fmt.Println(&quot;you done fucked up, boy&quot;)
  90. }
  91. content, er2 := ioutil.ReadAll(page.Body)
  92. //fmt.Println(content)
  93. if er2 != nil {
  94. fmt.Println(er2)
  95. }
  96. canal &lt;- string(content)
  97. }
  98. </details>
  99. # 答案1
  100. **得分**: 1
  101. 根据第一个评论的指示(在每次调用后不关闭通道,并将对工作函数的调用设置为goroutine),我现在将为您提供一个可工作的版本:
  102. ```go
  103. package main
  104. import (
  105. "fmt"
  106. "io/ioutil"
  107. "net/http"
  108. "os"
  109. )
  110. func main() {
  111. initial := "https://www1.medion.de/downloads/index.pl?op=detail&id="
  112. ending := "&type=treiber&lang=uk"
  113. links := []string{}
  114. os.Remove("dump.txt")
  115. dumpFile, _ := os.Create("dump.txt")
  116. c := make(chan string)
  117. for i := 16000; i < 16004; i++ {
  118. links = append(links, initial+fmt.Sprint(i)+ending)
  119. }
  120. fmt.Println(links[0])
  121. for _, link := range links {
  122. go getpageCanal(c, link)
  123. }
  124. for el := range c {
  125. fmt.Println(el)
  126. n, err := dumpFile.WriteString(el)
  127. if err != nil {
  128. fmt.Println(err)
  129. }
  130. if n == 0 {
  131. fmt.Println(" nothing written in main")
  132. }
  133. }
  134. }
  135. func getpageCanal(canal chan string, url string) {
  136. //defer close(canal)
  137. page, err := http.Get(url)
  138. if err != nil {
  139. fmt.Println("you done fucked up, boy")
  140. }
  141. content, er2 := ioutil.ReadAll(page.Body)
  142. //fmt.Println(content)
  143. if er2 != nil {
  144. fmt.Println(er2)
  145. }
  146. canal <- string(content)
  147. }

希望对您有所帮助!

英文:

After modifying the code as instructed by the first comments (not closing the channel after each call and making the call to the worker function a go routine) I will now provide you with a working version:

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;io/ioutil&quot;
  5. &quot;net/http&quot;
  6. &quot;os&quot;
  7. )
  8. func main() {
  9. initial := &quot;https://www1.medion.de/downloads/index.pl?op=detail&amp;id=&quot;
  10. ending := &quot;&amp;type=treiber&amp;lang=uk&quot;
  11. links := []string{}
  12. os.Remove(&quot;dump.txt&quot;)
  13. dumpFile, _ := os.Create(&quot;dump.txt&quot;)
  14. c := make(chan string)
  15. for i := 16000; i &lt; 16004; i++ {
  16. links = append(links, initial+fmt.Sprint(i)+ending)
  17. }
  18. fmt.Println(links[0])
  19. for _, link := range links {
  20. go getpageCanal(c, link)
  21. }
  22. for el := range c {
  23. fmt.Println(el)
  24. n, err := dumpFile.WriteString(el)
  25. if err != nil {
  26. fmt.Println(err)
  27. }
  28. if n == 0 {
  29. fmt.Println(&quot; nothing written in main&quot;)
  30. }
  31. }
  32. }
  33. func getpageCanal(canal chan string, url string) {
  34. //defer close(canal)
  35. page, err := http.Get(url)
  36. if err != nil {
  37. fmt.Println(&quot;you done fucked up, boy&quot;)
  38. }
  39. content, er2 := ioutil.ReadAll(page.Body)
  40. //fmt.Println(content)
  41. if er2 != nil {
  42. fmt.Println(er2)
  43. }
  44. canal &lt;- string(content)
  45. }

huangapple
  • 本文由 发表于 2021年10月31日 01:22:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/69780498.html
匿名

发表评论

匿名网友

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

确定