I wrote a simple web server to listen on port 8080. But I don't want to use a hard coded port number. What I want is that my server listen on any available port. And I want to know that on what port number my web server is listening.
My code is given bellow:
package main
import (
"net/http"
)
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}
You may use port
0
to indicate you're not specifying an exact port but you want a free, available port selected by the system:The problem with this is that you won't be able to find out what port was assigned. So you need to create the
net.Listener
yourself (using thenet.Listen()
function), and manually pass it tohttp.Serve()
:Example output:
As you can see, you can access the assigned port from the
net.Listener
, from itsnet.Addr
address (acquired by itsAddr()
method).net.Addr
does not directly give access to the port, but since we created thenet.Listener
usingtcp
network stream, thenet.Addr
will be of dynamic type*net.TCPAddr
(which we can acquire with a type assertion), which is a struct and has a fieldPort int
.Note that if you don't need the port in your application (e.g. you just want to display it for yourself), you don't need the type assertion, you can just print
listener.Addr()
(which will contain the port at the end):Example output:
Also don't forget to handle returned errors (
http.ListenAndServe()
in this case). In my example I simply passed it topanic()
becausehttp.LitenAndServe()
andhttp.Serve()
block if everything goes well (so they only return if there's an error, which I pass topanic()
).