add GetEnvDefault

This commit is contained in:
bryanqiu 2022-12-29 20:35:33 +08:00
parent 906d43322e
commit e21dbcc61b

17
str.go
View File

@ -6,6 +6,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"math/rand" "math/rand"
"os"
"strings" "strings"
"time" "time"
) )
@ -42,7 +43,7 @@ func GetRandomCharString(l int) string {
return string(result) return string(result)
} }
//skip // skip
func Skip(s string, sep string, n int) string { func Skip(s string, sep string, n int) string {
seplen := len(sep) seplen := len(sep)
if seplen == 0 || n < 0 { if seplen == 0 || n < 0 {
@ -104,8 +105,9 @@ func JsonstrToStrMap(jsonstr string) (m map[string]string) {
} }
// string to string map to string // string to string map to string
// example: "K1:V1;K2:V2" //
// => {K1:V1, K2:V2} // example: "K1:V1;K2:V2"
// => {K1:V1, K2:V2}
func StringToStrMap(str string, sep1, sep2 string) (m map[string]string) { func StringToStrMap(str string, sep1, sep2 string) (m map[string]string) {
m = map[string]string{} m = map[string]string{}
for _, kvstr := range strings.Split(str, sep1) { for _, kvstr := range strings.Split(str, sep1) {
@ -115,3 +117,12 @@ func StringToStrMap(str string, sep1, sep2 string) (m map[string]string) {
} }
return m return m
} }
// get os env with default
func GetEnvDefault(key, defval string) string {
if val, ok := os.LookupEnv(key); ok {
return val
} else {
return defval
}
}