go - 如何中断执行 (*TCPListener) Accept 的 goroutine?

标签 go

我最近在玩 go 并尝试创建一些服务器来响应 tcp 连接上的客户端。

我的问题是我如何干净地关闭服务器并中断当前在以下调用中“阻塞”的 go-routine

func (*TCPListener) Accept?

根据documentation of Accept

Accept implements the Accept method in the Listener interface; it waits for the next call and returns a generic Conn.

这些错误也很少被记录。

最佳答案

只需 Close() net.Listener您从 net.Listen(...) 调用中获取并从正在执行的 goroutine 返回。

关于go - 如何中断执行 (*TCPListener) Accept 的 goroutine?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18825207/

相关文章:

go - 在 golang 中读取二进制 .fbx 文件

go - 如何使用 golang 压缩和调整 gif 的大小

Golang检查字符串是否是有效路径

go - Go testing.B 基准测试是否可以防止不必要的优化?

go - 根据名称在 JSON Schema 中搜索 JSON 对象并获取路径

go - 解码动态 XML

go - 将 XML 属性解码为命名的嵌套结构

testing - 模拟 bufio.NewScanner 的标准输出流

go - 将通用结构/接口(interface)传递给函数并返回它

concurrency - Goroutines with ListenAndServe 提高性能?