aboutsummaryrefslogtreecommitdiff
path: root/go/read-unix-sock/read-unix-sock.go
blob: 2b09126263c8a9bff79ec8f7a0bece12ee9d806e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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]))
	}
}