Skip to content

std/io: make it easier to fine-grain std{in,out,err} redirection #171

@DeedleFake

Description

@DeedleFake

Being able globally change stdin, stdout, and stderr for the io module can be useful, but it doesn't work for some situations, such as running different scripts concurrently and putting the outputs in different buffers. Previously this could be remedied via sub-scoping the module, which is what the tests do, but now that io.panic exists this doesn't work. As a workaround, it's possible to specify an output for io.panic, but ideally it would just write to stderr. Unfortunately, it currently has no way to know what stderr is if it's been overridden via sub-scoping.

The only clean solution I can think of to this is to provide a function in std/io that returns a scope that's been modified so that all stdin, stdout, and stderr usages have been properly redirected. For example,

type MOptions struct {
  Stdin io.Reader
  Stdout io.Writer
  Stderr io.Writer
}

func M(opts *MOptions) *Scope

var S = M(nil)

Panic() would then need to be inserted into the scope in such a way that the default writer gets changed to whatever Stderr is declared to be.

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions