diff options
Diffstat (limited to 'go/read-unix-sock')
-rw-r--r-- | go/read-unix-sock/read-unix-sock.go | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/go/read-unix-sock/read-unix-sock.go b/go/read-unix-sock/read-unix-sock.go new file mode 100644 index 0000000..2b09126 --- /dev/null +++ b/go/read-unix-sock/read-unix-sock.go @@ -0,0 +1,30 @@ +package main + +import ( + "errors" + "fmt" + "io" + "io/fs" + "net" + "os" +) + +func main() { + info, _ := os.Stat(os.Args[1]) + if info.Mode().Type() != fs.ModeSocket { + fmt.Fprintln(os.Stderr, "File is not a socket") + os.Exit(1) + } + + c, _ := net.Dial("unix", os.Args[1]) + defer c.Close() + + buf := make([]byte, 1024) + for { + n, err := c.Read(buf) + if errors.Is(err, io.EOF) { + return + } + fmt.Print(string(buf[:n])) + } +} |