How can I check whether a file is a socket or not?

I search online but couldn’t figure out how to achieve it. Does anyone know how to check whether a file is a socket in Golang?

os.Stat returns a fs.FileInfo, which in turn has a field Mode of type fs.FileMode, it has the info you need.

So something like this should work:

s, _ := os.Stat("foo")
s.Mode && fs.ModeSocket == fs.ModeSocket

Thank you, NobbZ! Yep, I figured out something similar.

// Check if a file is a socket.
func IsSocket(path string) bool {
	fileInfo, err := os.Stat(path)
	if err != nil {
		log.Fatal("ERROR - ", err)
	}
	return fileInfo.Mode().Type() == fs.ModeSocket
}
2 Likes