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

builtin: description of close behavior of reading from closed channel is vague #53041

Closed
aaronbuchwald opened this issue May 23, 2022 · 1 comment
Labels
Documentation FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@aaronbuchwald
Copy link

aaronbuchwald commented May 23, 2022

What is the URL of the page with the issue?

https://pkg.go.dev/builtin#close

The documentation of the built in close function states explicitly:

The form

x, ok := <-c
will also set ok to false for a closed channel.

This comes after the sentence that states after every item has been read from the channel, but it does not seem clear to me that applies to this statement as well.

When reading from a closed channel that has unread elements in it. It seems intuitive that the behavior that currently exists should be the actual behavior of the language ie. reading from the channel should read all of the available elements, but the documentation currently suggests that is not the case and should be amended.

Wrote a small unit test:

func TestReadClosedChannel(t *testing.T) {
	c := make(chan struct{}, 1)

	c <- struct{}{}
	close(c)

	_, ok := <-c
	assert.True(t, ok)
}

It seems that this was always the intention of the language to work this way, so this issue is just to ask that the documentation be made a little more clear for the close function.

@gopherbot gopherbot added this to the Unreleased milestone May 23, 2022
@seankhliao seankhliao changed the title x/pkgsite: builtin close behavior of reading from closed channel is vague builtin: description of close behavior of reading from closed channel is vague May 23, 2022
@seankhliao seankhliao added Documentation NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. and removed pkgsite labels May 23, 2022
@gopherbot
Copy link

Change https://go.dev/cl/407975 mentions this issue: builtin: clarify effect of close on receive

@dmitshur dmitshur added NeedsFix The path to resolution is known, but the work has not been done. and removed NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels May 24, 2022
@dmitshur dmitshur modified the milestones: Unreleased, Go1.19 May 24, 2022
@golang golang locked and limited conversation to collaborators Feb 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Documentation FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

5 participants
@dmitshur @gopherbot @seankhliao @aaronbuchwald and others