Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/int128/listener
Go package to allocate a net.Listener from address candidates or free port
https://github.com/int128/listener
golang network
Last synced: 21 days ago
JSON representation
Go package to allocate a net.Listener from address candidates or free port
- Host: GitHub
- URL: https://github.com/int128/listener
- Owner: int128
- License: apache-2.0
- Created: 2019-10-03T00:57:58.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2024-09-25T07:04:56.000Z (about 2 months ago)
- Last Synced: 2024-10-13T00:56:22.400Z (about 1 month ago)
- Topics: golang, network
- Language: Go
- Homepage:
- Size: 16.6 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# listener ![build](https://github.com/int128/listener/workflows/build/badge.svg) [![GoDoc](https://godoc.org/github.com/int128/listener?status.svg)](https://godoc.org/github.com/int128/listener)
This is a Go package to allocate a net.Listener from address candidates.
```sh
go get github.com/int128/listener
```## Examples
### Allocate a free port
To allocate a `net.Listener` at a free port on localhost:
```go
package mainimport (
"fmt""github.com/int128/listener"
)func main() {
l, err := listener.New(nil)
if err != nil {
panic(err)
}
defer l.Close()fmt.Printf("Open %s", l.URL)
}
```### Allocate a port from candidates
To allocate a `net.Listener` at a port 18000 or 28000 on localhost:
```go
package mainimport (
"fmt""github.com/int128/listener"
)func main() {
l, err := listener.New([]string{"127.0.0.1:18000", "127.0.0.1:28000"})
if err != nil {
panic(err)
}
defer l.Close()fmt.Printf("Open %s", l.URL)
}
```If port 18000 is already in use, it will allocate port 28000.
## Contributions
This is an open source software.
Free free to open issues and pull requests.