github.com/fzfile/BaiduPCS-Go@v0.0.0-20200606205115-4408961cf336/pcsutil/addr.go (about)

     1  package pcsutil
     2  
     3  import (
     4  	"net"
     5  )
     6  
     7  // ListAddresses 列出本地可用的 IP 地址
     8  func ListAddresses() (addresses []string) {
     9  	iFaces, _ := net.Interfaces()
    10  	addresses = make([]string, 0, len(iFaces))
    11  	for k := range iFaces {
    12  		iFaceAddrs, _ := iFaces[k].Addrs()
    13  		for l := range iFaceAddrs {
    14  			switch v := iFaceAddrs[l].(type) {
    15  			case *net.IPNet:
    16  				addresses = append(addresses, v.IP.String())
    17  			case *net.IPAddr:
    18  				addresses = append(addresses, v.IP.String())
    19  			}
    20  		}
    21  	}
    22  	return
    23  }
    24  
    25  // ParseHost 解析地址中的host
    26  func ParseHost(address string) string {
    27  	h, _, err := net.SplitHostPort(address)
    28  	if err != nil {
    29  		return address
    30  	}
    31  	return h
    32  }