Go goroutine 与通道奇怪的结果

Go goroutine with channel strange result

当我 运行 goroutines 时,我通常得到 40 作为值,我知道它的并发性但为什么最后一个数字来了?我想输出必须是:

Page number:  34  
Page number:  12  
Page number:  8  
Page number:  2  
Page number:  29

示例源代码

package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
)

func getWebPageContent(url string, c chan int, val int) interface{} {

    if r, err := http.Get(url); err == nil {
        defer r.Body.Close()
        if body, err := ioutil.ReadAll(r.Body); err == nil {
            c <- val
            return string(body)
        }
    } else {
        fmt.Println(err)
    }
    return "XoX"

}

const MAX_TH = 40

func main() {

    // pln := fmt.Println
    messages := make(chan int)
    for j := 0; j < MAX_TH; j++ {
        go func() { getWebPageContent("http://www.example.com", messages, j) }()
    }

    routine_count := 0
    var page_number int
    for {
        page_number = <-messages
        routine_count++
        fmt.Println("Page number: ", page_number)
        if routine_count == MAX_TH {
            break
        }
    }
    close(messages)
}

The Go Programming Language

Frequently Asked Questions (FAQ)

What happens with closures running as goroutines?

Some confusion may arise when using closures with concurrency. Consider the following program:

func main() {
    done := make(chan bool)

    values := []string{"a", "b", "c"}
    for _, v := range values {
        go func() {
            fmt.Println(v)
            done <- true
        }()
    }

    // wait for all goroutines to complete before exiting
    for _ = range values {
        <-done
    }
}

One might mistakenly expect to see a, b, c as the output. What you'll probably see instead is c, c, c. This is because each iteration of the loop uses the same instance of the variable v, so each closure shares that single variable. When the closure runs, it prints the value of v at the time fmt.Println is executed, but v may have been modified since the goroutine was launched. To help detect this and other problems before they happen, run go vet.

To bind the current value of v to each closure as it is launched, one must modify the inner loop to create a new variable each iteration. One way is to pass the variable as an argument to the closure:

for _, v := range values {
    go func(u string) {
        fmt.Println(u)
        done <- true
    }(v)
}

In this example, the value of v is passed as an argument to the anonymous function. That value is then accessible inside the function as the variable u.

Even easier is just to create a new variable, using a declaration style that may seem odd but works fine in Go:

for _, v := range values {
    v := v // create a new 'v'.
    go func() {
        fmt.Println(v)
        done <- true
    }()
}

因此,在您的情况下,通过添加语句 j := j

创建一个新变量
for j := 0; j < MAX_TH; j++ {
    j := j
    go func() { getWebPageContent("http://www.example.com", messages, j) }()
}

例如,

package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
)

func getWebPageContent(url string, c chan int, val int) interface{} {
    if r, err := http.Get(url); err == nil {
        defer r.Body.Close()
        if body, err := ioutil.ReadAll(r.Body); err == nil {
            c <- val
            return string(body)
        }
    } else {
        fmt.Println(err)
    }
    return "XoX"
}

const MAX_TH = 40

func main() {

    // pln := fmt.Println
    messages := make(chan int)
    for j := 0; j < MAX_TH; j++ {
        j := j
        go func() { getWebPageContent("http://www.example.com", messages, j) }()
    }

    routine_count := 0
    var page_number int
    for {
        page_number = <-messages
        routine_count++
        fmt.Println("Page number: ", page_number)
        if routine_count == MAX_TH {
            break
        }
    }
    close(messages)
}

输出:

Page number:  23
Page number:  6
Page number:  1
Page number:  3
Page number:  28
Page number:  32
Page number:  18
Page number:  22
Page number:  0
Page number:  36
Page number:  7
Page number:  21
Page number:  12
Page number:  2
Page number:  5
Page number:  4
Page number:  33
Page number:  13
Page number:  20
Page number:  27
Page number:  29
Page number:  8
Page number:  31
Page number:  10
Page number:  17
Page number:  25
Page number:  19
Page number:  35
Page number:  14
Page number:  38
Page number:  15
Page number:  30
Page number:  37
Page number:  39
Page number:  26
Page number:  9
Page number:  16
Page number:  11
Page number:  24
Page number:  34

我的第一个 golang 回复,可能完全关闭:-)

循环可能看起来像这样:

...
for j := 0; j < MAX_TH; j++ {
    go func(x) { getWebPageContent("http://www.example.com", messages, x) }(j)
}
...

基本上,您定义一个匿名函数并使用参数调用它。你可以做不同的事情,但这个解决方案看起来非常实用和时尚:-)