代码拉取完成,页面将自动刷新
// Forked from github.com/StefanKopieczek/gossip by @StefanKopieczek
package util
import (
"errors"
"net"
"sync"
)
// Check two string pointers for equality as follows:
// - If neither pointer is nil, check equality of the underlying strings.
// - If either pointer is nil, return true if and only if they both are.
func StrPtrEq(a *string, b *string) bool {
if a == nil || b == nil {
return a == b
}
return *a == *b
}
// Check two uint16 pointers for equality as follows:
// - If neither pointer is nil, check equality of the underlying uint16s.
// - If either pointer is nil, return true if and only if they both are.
func Uint16PtrEq(a *uint16, b *uint16) bool {
if a == nil || b == nil {
return a == b
}
return *a == *b
}
func Coalesce(arg1 interface{}, arg2 interface{}, args ...interface{}) interface{} {
all := append([]interface{}{arg1, arg2}, args...)
for _, arg := range all {
if arg != nil {
return arg
}
}
return nil
}
func Noop() {}
func MergeErrs(chs ...<-chan error) <-chan error {
wg := new(sync.WaitGroup)
out := make(chan error)
pipe := func(ch <-chan error) {
defer wg.Done()
for err := range ch {
out <- err
}
}
wg.Add(len(chs))
for _, ch := range chs {
go pipe(ch)
}
go func() {
wg.Wait()
close(out)
}()
return out
}
func ResolveSelfIP() (net.IP, error) {
ifaces, err := net.Interfaces()
if err != nil {
return nil, err
}
for _, iface := range ifaces {
if iface.Flags&net.FlagUp == 0 {
continue // interface down
}
if iface.Flags&net.FlagLoopback != 0 {
continue // loopback interface
}
addrs, err := iface.Addrs()
if err != nil {
return nil, err
}
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if ip == nil || ip.IsLoopback() {
continue
}
ip = ip.To4()
if ip == nil {
continue // not an ipv4 address
}
return ip, nil
}
}
return nil, errors.New("server not connected to any network")
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。