]> Sergey Matveev's repositories - btrtrc.git/blob - util/addr.go
util/CopyExact: Test copying from interfaces and nil
[btrtrc.git] / util / addr.go
1 package util
2
3 import (
4         "net"
5         "strconv"
6 )
7
8 // Extracts the port as an integer from an address string.
9 func AddrPort(addr net.Addr) int {
10         _, port, err := net.SplitHostPort(addr.String())
11         if err != nil {
12                 panic(err)
13         }
14         i64, err := strconv.ParseInt(port, 0, 0)
15         if err != nil {
16                 panic(err)
17         }
18         return int(i64)
19 }
20
21 func AddrIP(addr net.Addr) net.IP {
22         host, _, err := net.SplitHostPort(addr.String())
23         if err != nil {
24                 panic(err)
25         }
26         return net.ParseIP(host)
27 }