package main import ( "net" "os" ) func main() { conn, err := net.DialUnix("unix", nil, &net.UnixAddr{Name: "/tmp/mysocket", Net: "unix"}) if err != nil { panic(err) } defer conn.Close() _, err = conn.Write([]byte("Hello, world!")) if err != nil { panic(err) } }
package main import ( "net" "os" ) func main() { l, err := net.ListenUnix("unix", &net.UnixAddr{Name: "/tmp/mysocket", Net: "unix"}) if err != nil { panic(err) } defer os.Remove("/tmp/mysocket") defer l.Close() for { conn, err := l.Accept() if err != nil { panic(err) } go handleConnection(conn) } } func handleConnection(conn net.Conn) { defer conn.Close() buffer := make([]byte, 1024) _, err := conn.Read(buffer) if err != nil { panic(err) } println(string(buffer)) }In this code example, the `net.ListenUnix` function is used to create a listener for incoming connections on the Unix domain socket located at `/tmp/mysocket`. When a client connects, the server uses the `handleConnection` function to read the message from the client and print it to the console. Package Library: net