chan

golang实现异步并发sokect

点点圈 提交于 2021-02-02 05:44:39
搜索golang + epoll的例子,得到下面这段代码,感觉golang的编程思维真正做到了并行编程: package main import ( "fmt" "net" "os" "time" ) const ( MAX_CONN_NUM = 5 ) //echo server Goroutine func EchoFunc(conn net.Conn) { defer conn.Close() buf := make([]byte, 1024) for { _, err := conn.Read(buf) if err != nil { //println("Error reading:", err.Error()) return } //send reply _, err = conn.Write(buf) if err != nil { //println("Error send reply:", err.Error()) return } } } //initial listener and run func main() { listener, err := net.Listen("tcp", "0.0.0.0:8088") if err != nil { fmt.Println("error listening:", err.Error()) os.Exit(1)