go语言使用gin启动端口失败。上代码:
func main() {
//创建路由
r :=gin.Default()
//绑定路由,执行的函数
//gin.Context,封装了request和respose
r.GET("/", func(c *gin.Context) {
c.String(http.StatusOK,"hello hello")
})
r.Run("8080")
}
控制台打印:
GOROOT=C:\Program Files (x86)\Go #gosetup
GOPATH=C:\Users\31916\go #gosetup
"C:\Program Files (x86)\Go\bin\go.exe" build -o C:\Users\31916\AppData\Local\Temp\___go_build_gostudy.exe gostudy #gosetup
C:\Users\31916\AppData\Local\Temp\___go_build_gostudy.exe #gosetup
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
- using env: export GIN_MODE=release
- using code: gin.SetMode(gin.ReleaseMode)
[GIN-debug] GET / --> main.main.func1 (3 handlers)
[GIN-debug] [WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.
Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.
[GIN-debug] Listening and serving HTTP on 8080
[GIN-debug] [ERROR] listen tcp: address 8080: missing port in address
Process finished with the exit code 0
解决办法:
一般是两种情况:
1.端口被占用(这种没遇到过)
2.仔细看代码,代码出错了。例如我的代码:
r.Run(“8080”) 改为 r.Run(“:8080”)