forked from Calcium-Ion/new-api
-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: wozulong <>
- Loading branch information
Showing
72 changed files
with
1,972 additions
and
1,176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package common | ||
|
||
import ( | ||
"encoding/json" | ||
"math/rand" | ||
"strconv" | ||
"unsafe" | ||
) | ||
|
||
func GetStringIfEmpty(str string, defaultValue string) string { | ||
if str == "" { | ||
return defaultValue | ||
} | ||
return str | ||
} | ||
|
||
func GetRandomString(length int) string { | ||
//rand.Seed(time.Now().UnixNano()) | ||
key := make([]byte, length) | ||
for i := 0; i < length; i++ { | ||
key[i] = keyChars[rand.Intn(len(keyChars))] | ||
} | ||
return string(key) | ||
} | ||
|
||
func MapToJsonStr(m map[string]interface{}) string { | ||
bytes, err := json.Marshal(m) | ||
if err != nil { | ||
return "" | ||
} | ||
return string(bytes) | ||
} | ||
|
||
func MapToJsonStrFloat(m map[string]float64) string { | ||
bytes, err := json.Marshal(m) | ||
if err != nil { | ||
return "" | ||
} | ||
return string(bytes) | ||
} | ||
|
||
func StrToMap(str string) map[string]interface{} { | ||
m := make(map[string]interface{}) | ||
err := json.Unmarshal([]byte(str), &m) | ||
if err != nil { | ||
return nil | ||
} | ||
return m | ||
} | ||
|
||
func String2Int(str string) int { | ||
num, err := strconv.Atoi(str) | ||
if err != nil { | ||
return 0 | ||
} | ||
return num | ||
} | ||
|
||
func StringsContains(strs []string, str string) bool { | ||
for _, s := range strs { | ||
if s == str { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
// StringToByteSlice []byte only read, panic on append | ||
func StringToByteSlice(s string) []byte { | ||
tmp1 := (*[2]uintptr)(unsafe.Pointer(&s)) | ||
tmp2 := [3]uintptr{tmp1[0], tmp1[1], tmp1[1]} | ||
return *(*[]byte)(unsafe.Pointer(&tmp2)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.