From 03f8544691a8de9f77a803fd8cd2eeadb919ec03 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Sun, 10 Dec 2023 00:39:16 +0100 Subject: Genesis commit --- go/read-unix-sock/read-unix-sock.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 go/read-unix-sock/read-unix-sock.go (limited to 'go/read-unix-sock') 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])) + } +} -- cgit v1.2.3