Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listen on TCP4 not TCP6

Tags:

go

ipv4

go-gin

goji

I am using https://github.com/gin-gonic/gin to write an http service But when I deploy it, it keeps deploying on tcp6(according to 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")
like image 595
user914584 Avatar asked Jul 26 '16 14:07

user914584


1 Answers

The documentation states

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

You can start the server directly using an http.Server the same way the http package does in ListenAndServe

server := &http.Server{Handler: r}
l, err := net.Listen("tcp4", addr)
if err != nil {
    log.Fatal(err)
}
err = server.Serve(l)
// ...
like image 171
JimB Avatar answered Nov 01 '22 01:11

JimB