service/vendor/github.com/apolloconfig/agollo/v4/utils/utils.go

75 lines
1.7 KiB
Go
Raw Normal View History

2023-12-21 22:17:40 +08:00
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package utils
import (
"net"
"os"
"reflect"
"sync"
)
const (
//Empty 空字符串
Empty = ""
)
var (
internalIPOnce sync.Once
internalIP = ""
)
//GetInternal 获取内部ip
func GetInternal() string {
internalIPOnce.Do(func() {
addrs, err := net.InterfaceAddrs()
if err != nil {
os.Stderr.WriteString("Oops:" + err.Error())
os.Exit(1)
}
for _, a := range addrs {
if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
internalIP = ipnet.IP.To4().String()
}
}
}
})
return internalIP
}
//IsNotNil 判断是否nil
func IsNotNil(object interface{}) bool {
return !IsNilObject(object)
}
//IsNilObject 判断是否空对象
func IsNilObject(object interface{}) bool {
if object == nil {
return true
}
value := reflect.ValueOf(object)
kind := value.Kind()
if kind >= reflect.Chan && kind <= reflect.Slice && value.IsNil() {
return true
}
return false
}