Je kunt niet meer dan 25 onderwerpen selecteren Onderwerpen moeten beginnen met een letter of nummer, kunnen streepjes bevatten ('-') en kunnen maximaal 35 tekens lang zijn.

112 regels
2.1 KiB

4 jaren geleden
package common
import (
"bytes"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
"io/ioutil"
"math/rand"
"os"
"os/exec"
"runtime"
"strconv"
"time"
)
func Rand(min, max int) int {
if min > max {
panic("min: min cannot be greater than max")
}
if int31 := 1<<31 - 1; max > int31 {
panic("max: max can not be greater than " + strconv.Itoa(int31))
}
if min == max {
return min
}
r := rand.New(rand.NewSource(time.Now().UnixNano()))
return r.Intn(max+1-min) + min
}
func GbkToUtf8(s []byte) ([]byte, error) {
reader := transform.NewReader(bytes.NewReader(s), simplifiedchinese.GBK.NewDecoder())
d, e := ioutil.ReadAll(reader)
if e != nil {
return nil, e
}
return d, nil
}
func Utf8ToGbk(s []byte) ([]byte, error) {
reader := transform.NewReader(bytes.NewReader(s), simplifiedchinese.GBK.NewEncoder())
d, e := ioutil.ReadAll(reader)
if e != nil {
return nil, e
}
return d, nil
}
func NewRandStr(length int) string {
s := []string{
4 jaren geleden
"a", "b", "c", "d", "e", "f",
"g", "h", "i", "j", "k", "l",
"m", "n", "o", "p", "q", "r",
"s", "t", "u", "v", "w", "x",
"y", "z", "A", "B", "C", "D",
"E", "F", "G", "H", "I", "J",
"K", "L", "M", "N", "O", "P",
"Q", "R", "S", "T", "U", "V",
"W", "X", "Y", "Z",
}
str := ""
for i := 1; i <= length; i++ {
4 jaren geleden
r := rand.New(rand.NewSource(time.Now().UnixNano()))
str += s[r.Intn(len(s)-1)]
4 jaren geleden
}
return str
}
func Substr(s string, start, end int) string {
strRune := []rune(s)
if start == -1 {
4 jaren geleden
return string(strRune[:end])
}
if end == -1 {
4 jaren geleden
return string(strRune[start:])
}
return string(strRune[start:end])
}
func IsDir(path string) bool {
s, err := os.Stat(path)
if err != nil {
return false
}
return s.IsDir()
}
func Exists(path string) bool {
_, err := os.Stat(path)
if err != nil {
if os.IsExist(err) {
return true
}
return false
}
return true
}
func OpenImage(file string) {
if runtime.GOOS == "windows" {
cmd := exec.Command("cmd", "/k", "start", file)
_ = cmd.Start()
} else {
if runtime.GOOS == "linux" {
cmd := exec.Command("eog", file)
_ = cmd.Start()
} else {
cmd := exec.Command("open", file)
_ = cmd.Start()
4 jaren geleden
}
}
}