Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix comment for closing channel #572

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

lxl-renren
Copy link

j, more := <-jobs, if channel jobs is closed, j is not always zero value, and more is also not always false.

Following are the test example.

package main

import (
"fmt"
"time"
)

func main() {
jobs := make(chan int, 5)
done := make(chan bool)

go func() {
	time.Sleep(time.Minute)
    for j := 1; j <= 2; j++ {
        j, more := <-jobs
        if more {
            fmt.Println("received job", j)
        } else {
            fmt.Println("received all jobs")
        }
    }
	done <- true
}()

for j := 1; j <= 3; j++ {
    jobs <- j
    fmt.Println("sent job", j)
}
close(jobs)
fmt.Println("sent all jobs")

<-done

t, ok := <-jobs
fmt.Println("received more jobs:", t, ok)

t, ok = <-jobs
fmt.Println("received more jobs:", t, ok)	

}

[lixiaolian@YBBJ-1100637deMacBook-Pro example]$go run valus.go
sent job 1
sent job 2
sent job 3
sent all jobs
received job 1
received job 2
received more jobs: 3 true
received more jobs: 0 false

`j, more := <-jobs`, if channel `jobs` is closed, j is not always zero value, and `more` is also not always false.

Following are the test example.

package main

import (
	"fmt"
	"time"
)

func main() {
    jobs := make(chan int, 5)
    done := make(chan bool)

    go func() {
		time.Sleep(time.Minute)
        for j := 1; j <= 2; j++ {
            j, more := <-jobs
            if more {
                fmt.Println("received job", j)
            } else {
                fmt.Println("received all jobs")
            }
        }
		done <- true
    }()

    for j := 1; j <= 3; j++ {
        jobs <- j
        fmt.Println("sent job", j)
    }
    close(jobs)
    fmt.Println("sent all jobs")

    <-done

    t, ok := <-jobs
    fmt.Println("received more jobs:", t, ok)

	t, ok = <-jobs
    fmt.Println("received more jobs:", t, ok)	
}


[lixiaolian@YBBJ-1100637deMacBook-Pro example]$go run valus.go
sent job 1
sent job 2
sent job 3
sent all jobs
received job 1
received job 2
received more jobs: 3 true
received more jobs: 0 false
@@ -17,7 +17,7 @@ func main() {
// Here's the worker goroutine. It repeatedly receives
// from `jobs` with `j, more := <-jobs`. In this
// special 2-value form of receive, the `more` value
// will be `false` if `jobs` has been `close`d and all
// will be `false` if all
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I don't understand this change. The comment seems to be in line with the spec https://go.dev/ref/spec#Receive_operator

Can you clarify what's wrong with this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants