type onceError … func (a *onceError) Store(err error) { … } func (a *onceError) Load() error { … } var ErrClosedPipe … type pipe … func (p *pipe) read(b []byte) (n int, err error) { … } func (p *pipe) closeRead(err error) error { … } func (p *pipe) write(b []byte) (n int, err error) { … } func (p *pipe) closeWrite(err error) error { … } // readCloseError is considered internal to the pipe type. func (p *pipe) readCloseError() error { … } // writeCloseError is considered internal to the pipe type. func (p *pipe) writeCloseError() error { … } type PipeReader … // Read implements the standard Read interface: // it reads data from the pipe, blocking until a writer // arrives or the write end is closed. // If the write end is closed with an error, that error is // returned as err; otherwise err is EOF. func (r *PipeReader) Read(data []byte) (n int, err error) { … } // Close closes the reader; subsequent writes to the // write half of the pipe will return the error [ErrClosedPipe]. func (r *PipeReader) Close() error { … } // CloseWithError closes the reader; subsequent writes // to the write half of the pipe will return the error err. // // CloseWithError never overwrites the previous error if it exists // and always returns nil. func (r *PipeReader) CloseWithError(err error) error { … } type PipeWriter … // Write implements the standard Write interface: // it writes data to the pipe, blocking until one or more readers // have consumed all the data or the read end is closed. // If the read end is closed with an error, that err is // returned as err; otherwise err is [ErrClosedPipe]. func (w *PipeWriter) Write(data []byte) (n int, err error) { … } // Close closes the writer; subsequent reads from the // read half of the pipe will return no bytes and EOF. func (w *PipeWriter) Close() error { … } // CloseWithError closes the writer; subsequent reads from the // read half of the pipe will return no bytes and the error err, // or EOF if err is nil. // // CloseWithError never overwrites the previous error if it exists // and always returns nil. func (w *PipeWriter) CloseWithError(err error) error { … } // Pipe creates a synchronous in-memory pipe. // It can be used to connect code expecting an [io.Reader] // with code expecting an [io.Writer]. // // Reads and Writes on the pipe are matched one to one // except when multiple Reads are needed to consume a single Write. // That is, each Write to the [PipeWriter] blocks until it has satisfied // one or more Reads from the [PipeReader] that fully consume // the written data. // The data is copied directly from the Write to the corresponding // Read (or Reads); there is no internal buffering. // // It is safe to call Read and Write in parallel with each other or with Close. // Parallel calls to Read and parallel calls to Write are also safe: // the individual calls will be gated sequentially. func Pipe() (*PipeReader, *PipeWriter) { … }