blob: 8323fa37b66498dd99863e7d9e124d265ff67cdd [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001package swag
2
3import (
4 "net"
5 "strconv"
6)
7
8// SplitHostPort splits a network address into a host and a port.
9// The port is -1 when there is no port to be found
10func SplitHostPort(addr string) (host string, port int, err error) {
11 h, p, err := net.SplitHostPort(addr)
12 if err != nil {
13 return "", -1, err
14 }
15 if p == "" {
16 return "", -1, &net.AddrError{Err: "missing port in address", Addr: addr}
17 }
18
19 pi, err := strconv.Atoi(p)
20 if err != nil {
21 return "", -1, err
22 }
23 return h, pi, nil
24}