fix race in sample code

Don't close the pty directly; instead send an EOT to
cause the terminal to indicate end-of-file in the
slave device. Closing the pty caused io.Copy to return
early. Fixes #7.
1 file changed
tree: 45a05beabc7141f8d2ba396d5cb9c04c00d89ba8
  1. .gitignore
  2. License
  3. pty_darwin.go
  4. pty_linux.go
  5. README.md
  6. run.go
README.md

pty

Pty is a Go package for using unix pseudo-terminals.

(Note, there is only a Linux implementation. I'd appreciate a patch for other systems!)

Install

go get github.com/kr/pty

Example

package main

import (
	"github.com/kr/pty"
	"io"
	"os"
	"os/exec"
)

func main() {
	c := exec.Command("grep", "--color=auto", "bar")
	f, err := pty.Start(c)
	if err != nil {
		panic(err)
	}

	go func() {
		f.Write([]byte("foo\n"))
		f.Write([]byte("bar\n"))
		f.Write([]byte("baz\n"))
		f.Write([]byte{4}) // EOT
	}()
	io.Copy(os.Stdout, f)
}