Go 转到excute http.ListenAndServe(“8080”,nil),但进程退出 主程序包 进口( “io” “net/http” ) func main(){ http.HandleFunc(“/user/login”,func(writer http.ResponseWriter,request*http.request){ io.WriteString(作家,“你好,世界”) }) //启动一个服务器 http.ListenAndServe(“8080”,无) }
我想监听8080端口,但当我运行此代码时,它是exit auto请参见:Go 转到excute http.ListenAndServe(“8080”,nil),但进程退出 主程序包 进口( “io” “net/http” ) func main(){ http.HandleFunc(“/user/login”,func(writer http.ResponseWriter,request*http.request){ io.WriteString(作家,“你好,世界”) }) //启动一个服务器 http.ListenAndServe(“8080”,无) },go,Go,我想监听8080端口,但当我运行此代码时,它是exit auto请参见: listendandserve的正确语法是listendandserve(“:8080”,nil) 如果添加带有log.Fatal(http.ListenAndServe(“8080”,nil))的日志,您可能会看到错误: 2020/11/12 14:44:11侦听tcp:地址8080:地址中缺少端口 您的侦听端口地址可能格式错误。下面的代码片段很有用 package main import (
listendandserve
的正确语法是listendandserve(“:8080”,nil)
如果添加带有log.Fatal(http.ListenAndServe(“8080”,nil))
的日志,您可能会看到错误:
2020/11/12 14:44:11侦听tcp:地址8080:地址中缺少端口
您的侦听端口地址可能格式错误。下面的代码片段很有用
package main
import (
"fmt"
"net/http"
)
func main() {
http.HandleFunc("/", Helloworld)
http.ListenAndServe(":8080", nil)
}
func Helloworld(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %s!", r.URL.Path[1:])
}
http.ListenAndServe(“8080”,nil)
返回错误。尝试打印它以更好地理解失败的原因,例如,fmt.Println(http.ListenAndServe(“8080”,nil))
将打印nil
,如果工作正常,否则将打印实际错误。