83 lines
1.6 KiB
Go
83 lines
1.6 KiB
Go
package util
|
|
|
|
import (
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
"sync"
|
|
|
|
jsoniter "github.com/json-iterator/go"
|
|
)
|
|
|
|
const MacOSChromeUA = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
|
|
|
|
var (
|
|
Json = jsoniter.ConfigCompatibleWithStandardLibrary
|
|
)
|
|
|
|
func IsWindows() bool {
|
|
return os.PathSeparator == '\\' && os.PathListSeparator == ';'
|
|
}
|
|
|
|
func BrowserHeaders() http.Header {
|
|
return http.Header{
|
|
"Accept": {"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"},
|
|
"Accept-Language": {"en,zh-CN;q=0.9,zh;q=0.8"},
|
|
"User-Agent": {MacOSChromeUA},
|
|
}
|
|
}
|
|
|
|
func ContainsStr(slice []string, str string) bool {
|
|
if str != "" {
|
|
for _, item := range slice {
|
|
if strings.Contains(str, item) {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func RemoveDuplicate[T comparable](sliceList []T) []T {
|
|
allKeys := make(map[T]bool)
|
|
list := []T{}
|
|
for _, item := range sliceList {
|
|
if _, value := allKeys[item]; !value {
|
|
allKeys[item] = true
|
|
list = append(list, item)
|
|
}
|
|
}
|
|
return list
|
|
}
|
|
|
|
// OnceValue returns a function that invokes f only once and returns the value
|
|
// returned by f. The returned function may be called concurrently.
|
|
//
|
|
// If f panics, the returned function will panic with the same value on every call.
|
|
func OnceValue[T any](f func() T) func() T {
|
|
var (
|
|
once sync.Once
|
|
valid bool
|
|
p any
|
|
result T
|
|
)
|
|
g := func() {
|
|
defer func() {
|
|
p = recover()
|
|
if !valid {
|
|
panic(p)
|
|
}
|
|
}()
|
|
result = f()
|
|
f = nil
|
|
valid = true
|
|
}
|
|
return func() T {
|
|
once.Do(g)
|
|
if !valid {
|
|
panic(p)
|
|
}
|
|
return result
|
|
}
|
|
}
|