Listening to Random Available Port in Go

Photo by @mbaumi on UnsplashTo use a random available port in Golang, you can use :0. I believe the port 0 would works for another language as well as I tried in python.$ python -m SimpleHTTPServer 0Serving HTTP on 0.0.0.0 port 43481 …According to li…


This content originally appeared on Level Up Coding - Medium and was authored by Clavin June

Photo by @mbaumi on Unsplash
Photo by @mbaumi on Unsplash

To use a random available port in Golang, you can use :0. I believe the port 0 would works for another language as well as I tried in python.

$ python -m SimpleHTTPServer 0
Serving HTTP on 0.0.0.0 port 43481 ...

According to lifewire, port 0 is a non-ephemeral port that works as a wildcard that tells the system to find any available ports particularly in the Unix OS.

The Go Code

package main
import (
"log"
"net"
"net/http"
)
func createListener() (l net.Listener, close func()) {
l, err := net.Listen("tcp", ":0")
if err != nil {
panic(err)
}
return l, func() {
_ = l.Close()
}
}
func main() {
l, close := createListener()
defer close()
http.Handle("/", http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
// handle like normal
}))
log.Println("listening at", l.Addr().(*net.TCPAddr).Port)
http.Serve(l, nil)
}

Execute it:

$ go run main.go 
2022/01/04 17:40:16 listening at 33845

Thank you for reading!

Originally published at https://clavinjune.dev on January 4, 2022.


Listening to Random Available Port in Go was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Clavin June


Print Share Comment Cite Upload Translate Updates
APA

Clavin June | Sciencx (2022-01-04T14:59:20+00:00) Listening to Random Available Port in Go. Retrieved from https://www.scien.cx/2022/01/04/listening-to-random-available-port-in-go/

MLA
" » Listening to Random Available Port in Go." Clavin June | Sciencx - Tuesday January 4, 2022, https://www.scien.cx/2022/01/04/listening-to-random-available-port-in-go/
HARVARD
Clavin June | Sciencx Tuesday January 4, 2022 » Listening to Random Available Port in Go., viewed ,<https://www.scien.cx/2022/01/04/listening-to-random-available-port-in-go/>
VANCOUVER
Clavin June | Sciencx - » Listening to Random Available Port in Go. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/01/04/listening-to-random-available-port-in-go/
CHICAGO
" » Listening to Random Available Port in Go." Clavin June | Sciencx - Accessed . https://www.scien.cx/2022/01/04/listening-to-random-available-port-in-go/
IEEE
" » Listening to Random Available Port in Go." Clavin June | Sciencx [Online]. Available: https://www.scien.cx/2022/01/04/listening-to-random-available-port-in-go/. [Accessed: ]
rf:citation
» Listening to Random Available Port in Go | Clavin June | Sciencx | https://www.scien.cx/2022/01/04/listening-to-random-available-port-in-go/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.