I'm unable to get a pipe state in Go (1.5).
While writing on a mkfifo created pipe, I try to get the state of this output pipe:
- using the Writereturn statusEPIPE
- using the Writereturn statusEPIPEandsignal.Ignoreon SIGPIPE (just in case)
- using signal.Notifyon SIGPIPE
I can see that:
- EPIPEis never returned
- when I use kill -13, the signal handler is called: "Got signal: broken pipe"
- when I ctrl-cthe reader, the signal handler is not called and my program exits with output: "signal: broken pipe"
Would you, please, indicate my error ?
// tee.go
package main
import (
    "fmt"
    "os"
    "os/signal"
    "syscall"
    sys "golang.org/x/sys/unix"
)
// wait for a signal and print it
func handleSignal(csig chan os.Signal) {
    for {
        fmt.Println("Wait signal")
        s := <-csig
        fmt.Println("Got signal:", s)
    }
}
func main() {
    csig := make(chan os.Signal, 1)
    // `kill -13` outputs "Got signal: broken pipe" => ok
    signal.Notify(csig, sys.SIGPIPE)
    // OR disable the previous `Notify` just to be sure ?
    // maybe it will help to get the EPIPE error status on `Write` ?
    //signal.Ignore(sys.SIGPIPE)
    go handleSignal(csig)
    // open previously created named pipe (`mkfifo /tmp/test`)
    pipe, _ := os.OpenFile("/tmp/test", os.O_WRONLY, 0)
    for {
        _, err := pipe.Write([]byte("foo\n"))
        if err == syscall.EPIPE {
            // never called => ko
            fmt.Println("EPIPE error")
        }
    }
}
Note: as a simple Go exercise, I try to implement a command which almost acts like tee -a <a_file> (print stdin to stdout and <a_file>) with the following specificity: non blocking write on a named pipe and optional reader.
 
     
    