golang 通道的分段违规

Tim*_*ell 2 channel go

下面的代码打开 10,000 个 go 例程,这些例程进行 HTTP 调用、获取响应、关闭响应,并使用 ID 写入通道。

在第二个 for 循环中,它从该缓冲通道中打印出前一个 go 例程的 ID。

这会导致分段违规,我无法弄清楚原因。

恐慌:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x40 pc=0x2293]
Run Code Online (Sandbox Code Playgroud)

代码:

package main

import (
    "fmt"
    "net/http"
)

func main() {
    requests := 10000
    ch := make(chan string, requests)
    for i := 1; i <= requests; i++ {
        go func(iter int) {
            fmt.Println(iter)
            resp, _ := http.Get("http://localhost:8080/api/project")
            resp.Body.Close()
            ch <- fmt.Sprint("%i", iter)
        }(i)
    }
    for i := 1; i <= requests; i++ {
        fmt.Println(<-ch)
    }
}
Run Code Online (Sandbox Code Playgroud)

Jea*_*sen 6

调用 api 时不检查任何错误。因此,尝试关闭从未到达的响应时出现错误。

这段代码不会惊慌:

package main

import (
    "fmt"
    "net/http"
)

func main() {
    requests := 10000
    ch := make(chan string, requests)
    for i := 1; i <= requests; i++ {
        go func(iter int) {
            fmt.Println(iter)
            resp, err := http.Get("http://localhost:8080/api/project")
            if (err == nil) {
              resp.Body.Close()
            }
            ch <- fmt.Sprint(iter)
        }(i)
    }
    for i := 1; i <= requests; i++ {
        fmt.Println(<-ch)
    }
}
Run Code Online (Sandbox Code Playgroud)