Matthias Andreas Benkard | 832a54e | 2019-01-29 09:27:38 +0100 | [diff] [blame^] | 1 | package swag |
| 2 | |
| 3 | import ( |
| 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 |
| 10 | func 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 | } |