Skip to content

proposal: Go2: allow send on a closed channel to return status on select #28017

Closed
@OneOfOne

Description

@OneOfOne

I have proposed this before (#15411), but at the time Go2 wasn't anywhere near planned.

The problem

  1. In multi-writers/readers situations, the user has to use an extra channel or context.Context.
  2. Users either implement their minimal version of Context or use Context with the slight overhead just as a safe channel barrier.

The proposal

  1. Allow close(ch) to always succeed.
  2. Allow:
select {
   case ok := ch <- val:
      if !ok { exit logic }
   case ch <- val: // this still panics if the channel is closed.
}

Go 1 compat

The the 2nd part of the proposal is 100% compatible with Go1, it will not break any current code since the syntax wouldn't be used in any older projects, the 1st part however can be iffy if you have tools that depend on the panic (I don't know any)

This also can also solve #27982.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions