【go】请教用go建立的协程为什么不能一直运行?
初学golang 搭建一个http服务,使用go建立协程后,运行一次就会停止
package mainimport (
"net/http"
)
func main() {
go server() //使用go新建一个协程
}
func server() {
http.ListenAndServe(":8080", nil)
}
请问如何保持一直连接?感谢回复~
你主线程都结束了,协程肯定也结束了啊,你要让主线程堵塞住才行。
package mainimport (
"net/http"
)
func main() {
go server() //使用go新建一个协程
select{}
}
func server() {
http.ListenAndServe(":8080", nil)
}
回答
以上是 【go】请教用go建立的协程为什么不能一直运行? 的全部内容, 来源链接: www.h5w3.com/114473.html