Golang net.Pipe() function example
package net
Golang net.Pipe() function usage example
package main
import (
"net"
)
func Client(c net.Conn) { ... }
func Server(c net.Conn) { ... }
func main() {
c1, c2 := net.Pipe()
go Server(c1)
Client(c2)
msg := "Message to client"
_, err := c1.Write([]byte(msg))
if err != nil {
fmt.Printf("Unable to send message : %v\n", err)
break
} else {
fmt.Printf("Sent : %s", msg)
msg = ""
}
}
NOTE : You can use net.Listener to setup net.Conn as well. For example :
func AcceptConnections(listener net.Listener) {
conn, err := listener.Accept()
if err != nil {
panic(err)
}
fmt.Printf("client connected\n")
....
}
Reference :
Advertisement
Something interesting
Tutorials
+26.7k Golang : How to check if a connection to database is still alive ?
+4.7k Linux/MacOSX : How to symlink a file?
+7.7k Golang : Generate human readable password
+35.5k Golang : Smarter Error Handling with strings.Contains()
+32.5k Golang : Copy directory - including sub-directories and files
+6.2k Golang : Calculate US Dollar Index (DXY)
+5.7k Golang : Struct field tags and what is their purpose?
+17.4k Golang : Get future or past hours, minutes or seconds
+30.8k Golang : Download file example
+8.6k Golang : Convert(cast) []byte to io.Reader type
+9k Golang : Build and compile multiple source files
+14.6k Golang : Execute function at intervals or after some delay