I need to know if a unix socket (specifically redis socket) is listening using GoLang in an Ubuntu machine. The only way I thought to do this is parsing the
netstat --listen
command output. Is there any other way?
I need to know if a unix socket (specifically redis socket) is listening using GoLang in an Ubuntu machine. The only way I thought to do this is parsing the
netstat --listen
command output. Is there any other way?
You could give a try to net.Dial, for example:
https://play.golang.org/p/DIsPzOmWqED
package main
import (
"fmt"
"log"
"net"
"path/filepath"
)
func main() {
l, err := net.Dial("unix", filepath.Join("/tmp", "my.sock"))
if err != nil {
log.Fatal(err)
}
fmt.Printf("l = %+v
", l)
}
If socket is not "listening" you may get an error like this:
2019/08/19 10:20:38 dial unix /tmp/my.sock: connect: connection refused
exit status 1