2 回答

TA貢獻1851條經驗 獲得超3個贊
您需要從頭開始創建服務器結構!
不能再使用關閉http.Server。
func serve() *http.Server {
router := gin.Default()
router.GET("/", func(c *gin.Context) {
time.Sleep(5 * time.Second)
c.String(http.StatusOK, "Welcome Gin Server\n")
})
srv := &http.Server{
Addr: ":8080",
Handler: router,
}
go func() {
// service connections
if err := srv.ListenAndServe(); err != nil {
log.Printf("listen: %s\n", err)
}
}()
return srv
}
func main() {
{
srv := serve()
time.Sleep(time.Second * 3)
fmt.Println("down", srv.Shutdown(context.Background()))
}
{
time.Sleep(time.Second * 3)
fmt.Println("up", serve())
}
select {}
}

TA貢獻1804條經驗 獲得超2個贊
我嘗試了很多方法,最后寫了一篇文章,這將幫助您解決問題: https ://dev.to/arshamalh/trying-to-shutdown-a-gin-server-goroutines-and-channels-提示-gf3
- 2 回答
- 0 關注
- 169 瀏覽
添加回答
舉報