gpt4 book ai didi

http - 使用 http.ListenAndServe 的 Golang 端口阻塞(?)

转载 作者:IT王子 更新时间:2023-10-29 02:34:09 30 4
gpt4 key购买 nike

我有一个简单的 golang 程序,它在执行名为 testFunc 的函数之前监听端口上的事件

func main() {
http.HandleFunc("/test", testFunc)
http.ListenAndServe(":1337", nil)
}

当我构建这个程序时,运行它并转到 http://localhost:1337/test ,它工作正常。

当我终止程序并再次尝试运行时,程序立即终止,没有错误输出。

例如,当我将端口更改为 1338 时,它第一次工作,然后每次都失败。有什么想法吗?

最佳答案

端口很可能正在使用中。捕获错误将为您提供更多详细信息。

if err := http.ListenAndServe(":1337", nil);err != nil {
log.Fatal("ListenAndServe: ", err)
}

关于http - 使用 http.ListenAndServe 的 Golang 端口阻塞(?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29286860/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com