gpt4 book ai didi

go - 监听 TCP4 而不是 TCP6

转载 作者:IT王子 更新时间:2023-10-29 00:36:45 54 4
gpt4 key购买 nike

我正在使用 https://github.com/gin-gonic/gin写一个http服务但是当我部署它时,它一直在 tcp6 上部署(根据 netstat)

r := gin.Default()
//none of these are working , It keeps being listed on tcp6
r.Run(":8080")
r.Run("*:8080")
r.Run("0.0.0.0:8080")

最佳答案

文档说明

Run attaches the router to a http.Server and starts listening and serving HTTP requests. It is a shortcut for http.ListenAndServe(addr, router)

您可以使用http.Server 直接启动服务器,就像http 包在ListenAndServe

中所做的一样
server := &http.Server{Handler: r}
l, err := net.Listen("tcp4", addr)
if err != nil {
log.Fatal(err)
}
err = server.Serve(l)
// ...

关于go - 监听 TCP4 而不是 TCP6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38592064/

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