我用gin測試時,端口無法正常啟動: [ERROR] listen tcp :8080: bind: address already in use當我用route修改端口時,還是顯示8080端口被占用func main() { //r := gin.Default() //r.GET("/ping", func(c *gin.Context) { // c.JSON(http.StatusOK, gin.H{ // "message": "pong", // }) //}) router := gin.Default() router.GET("/hi", func(context *gin.Context) { context.String(http.StatusOK, "Hello world!") }) err := router.Run() if err != nil { panic("[Error] failed to start Gin server due to: " + err.Error()) return } router.Run(":9888") //r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")}我應該如何修改它
2 回答

POPMUISE
TA貢獻1765條經驗 獲得超5個贊
您正在調用Run()兩次 - 在沒有提供任何地址的情況下調用第一個實例。所以在這個實例中使用默認端口 8080。更新代碼以在第一次調用中提供地址,并刪除重復調用應該有望為您解決此問題。
func main() {
router := gin.Default()
router.GET("/hi", func(context *gin.Context) {
context.String(http.StatusOK, "Hello world!")
})
err := router.Run(":9888")
if err != nil {
panic("[Error] failed to start Gin server due to: " + err.Error())
return
}
}
- 2 回答
- 0 關注
- 920 瀏覽
添加回答
舉報
0/150
提交
取消