自动化测试
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

127 lines
2.3 KiB

package main
import (
"net"
"regexp"
"sort"
"strings"
"time"
)
func In(target string, str_array []string) bool {
target = strings.ToUpper(target)
sort.Strings(str_array)
index := sort.SearchStrings(str_array, target)
if index < len(str_array) && str_array[index] == target {
return true
}
return false
}
type Delay struct {
flag bool
timer *time.Ticker
}
func DelayMany(t time.Duration, method func()) *Delay {
del := Delay{
flag: true,
timer: time.NewTicker(t),
}
go func() {
for del.flag {
select {
case <-del.timer.C:
method()
default:
time.Sleep(time.Millisecond)
}
}
}()
return &del
}
func (s *Delay) Close() {
s.flag = false
s.timer.Stop()
}
func DelayOnce(t time.Duration, method func()) *time.Timer {
timer := time.NewTimer(t)
go func() {
<-timer.C
method()
timer.Stop()
}()
return timer
}
func getLocalIPv4Address() (ipv4Address string, err error) {
netInterfaces, err := net.Interfaces()
if err != nil {
return "", err
}
for i := 0; i < len(netInterfaces); i++ {
if (netInterfaces[i].Flags & net.FlagUp) != 0 {
addrs, _ := netInterfaces[i].Addrs()
for _, address := range addrs {
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
// fmt.Println(ipnet.IP.String())
return ipnet.IP.String(), nil
}
}
}
}
}
return
}
func GetIpAddrCount() string {
ip, _ := getLocalIPv4Address()
p := regexp.MustCompile(`\d+\.\d+\.\d+\.(\d+)`).FindAllStringSubmatch(ip, -1)
if len(p) > 0 && len(p[0]) > 1 {
return p[0][1]
}
return ""
}
//该方法char参数只处理第一个字符,其他字符不处理
func charToByte(b byte) byte {
if b >= 97 && b <= 102 {
b = b - 97 + 10
} else if b >= 65 && b <= 70 {
b = b - 65 + 10
} else if b >= 48 && b <= 57 {
b = b - 48
} else {
b = 0
}
return b
}
func StrToHex(str string) []byte {
var bytes = make([]byte, 0)
endflag := false
for i := 0; i < len(str); i += 2 {
s0 := charToByte(str[i])
s1 := s0
if i+1 < len(str) {
s1 = charToByte(str[i+1])
} else {
s1 = 0
endflag = true
}
if !endflag {
bytes = append(bytes, s0<<4|s1)
} else {
bytes = append(bytes, s0)
}
}
return bytes
}
func TernaryVal(f bool, a, b interface{}) interface{} {
if f {
return a
}
return b
}