ln, err := net.Listen("tcp", ":8080")
if err != nil {
// handle error
}
for {
conn, err := ln.Accept()
if err != nil {
// handle error
}
go handleConnection(conn)
}
这是 net package 中提供的例子 其中的 conn 虽然操作是“阻塞”式的,但底层的 fd 都是 non-blocking 的,由 go runtime 调用 epoll 等机制监听并唤醒 goroutine 进行操作,如果将不耗时的业务也放在该 goroutine 中,耗时的业务辅以其他的异步处理,这样做会有什么问题?在实际的工程中,会怎样操作?需要在业务层实现一套 I/O 多路复用的机制吗?