You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/core/iox/pipe.go

24 lines
374 B
Go

4 years ago
package iox
import "os"
// RedirectInOut redirects stdin to r, stdout to w, and callers need to call restore afterward.
4 years ago
func RedirectInOut() (restore func(), err error) {
var r, w *os.File
r, w, err = os.Pipe()
if err != nil {
return
}
ow := os.Stdout
os.Stdout = w
or := os.Stdin
os.Stdin = r
restore = func() {
os.Stdin = or
os.Stdout = ow
}
return
}