Initial import
This commit is contained in:
35
internal/netutil/addr.go
Normal file
35
internal/netutil/addr.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package netutil
|
||||
|
||||
import (
|
||||
"net"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// EnsurePort makes sure the address in [host] contains a port.
|
||||
func EnsurePort(host, port string) string {
|
||||
if _, _, err := net.SplitHostPort(host); err == nil {
|
||||
return host
|
||||
}
|
||||
return net.JoinHostPort(host, port)
|
||||
}
|
||||
|
||||
// Host returns the bare host (without port).
|
||||
func Host(name string) string {
|
||||
host, _, err := net.SplitHostPort(name)
|
||||
if err == nil {
|
||||
return host
|
||||
}
|
||||
return name
|
||||
}
|
||||
|
||||
// Port returns the port number.
|
||||
func Port(name string) int {
|
||||
_, port, err := net.SplitHostPort(name)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
// TODO: name resolution for ports?
|
||||
i, _ := strconv.Atoi(port)
|
||||
return i
|
||||
}
|
Reference in New Issue
Block a user