package goku_node import ( "bytes" "math/rand" "runtime" "strings" "time" ) //生成随机字符串 func GetRandomString(num int) string { str := "123456789abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ" bytes := []byte(str) result := []byte{} r := rand.New(rand.NewSource(time.Now().UnixNano())) for i := 0; i < num; i++ { result = append(result, bytes[r.Intn(len(bytes))]) } return string(result) } func Intercept(str, substr string) string { result := strings.Index(str, substr) var rs string if result != -1 { rs = str[:result] } else { rs = str } return rs } func PanicTrace(kb int) []byte { s := []byte("/src/runtime/panic.go") e := []byte("\ngoroutine ") line := []byte("\n") stack := make([]byte, kb<<10) //4KB length := runtime.Stack(stack, true) start := bytes.Index(stack, s) stack = stack[start:length] start = bytes.Index(stack, line) + 1 stack = stack[start:] end := bytes.LastIndex(stack, line) if end != -1 { stack = stack[:end] } end = bytes.Index(stack, e) if end != -1 { stack = stack[:end] } stack = bytes.TrimRight(stack, "\n") return stack }