func (erw *EmulatorReadWriteCloser) Close() error {
	if erw.conn == nil {
		return fmt.Errorf("Cannot call Close when no connection is open")
	}
	err := erw.conn.Close()
	erw.conn = nil
	return err
}
Close implements io.Closer by closing the Unix domain socket if one is open.
package tpmutil
import (
	"fmt"
	"io"
	"net"
	"os"
)
func OpenTPM(path string) (io.ReadWriteCloser, error) {
	var rwc io.ReadWriteCloser
	fi, err := os.Stat(path)
	if err != nil {
		return nil, err
	}
	if fi.Mode()&os.ModeDevice != 0 {
		var f *os.File
		f, err = os.OpenFile(path, os.O_RDWR, 0600)
		if err != nil {
			return nil, err
		}
		rwc = io.ReadWriteCloser(f)
	} else if fi.Mode()&os.ModeSocket != 0 {
		rwc = NewEmulatorReadWriteCloser(path)
	} else {
		return nil, fmt.Errorf("unsupported TPM file mode %s", fi.Mode().String())
	}
	return rwc, nil
}
type dialer func(network, path string) (net.Conn, error)
type EmulatorReadWriteCloser struct {
	path   string
	conn   net.Conn
	dialer dialer
}
func NewEmulatorReadWriteCloser(path string) *EmulatorReadWriteCloser {
	return &EmulatorReadWriteCloser{
		path:   path,
		dialer: net.Dial,
	}
}
func (erw *EmulatorReadWriteCloser) Read(p []byte) (int, error) {
	if erw.conn == nil {
		return 0, fmt.Errorf("Must call Write then Read in an alternating sequence")
	}
	n, err := erw.conn.Read(p)
	erw.conn.Close()
	erw.conn = nil
	return n, err
}
func (erw *EmulatorReadWriteCloser) Write(p []byte) (int, error) {
	if erw.conn != nil {
		return 0, fmt.Errorf("Must call Write then Read in an alternating sequence")
	}
	var err error
	erw.conn, err = erw.dialer("unix", erw.path)
	if err != nil {
		return 0, err
	}
	return erw.conn.Write(p)
}