- update
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/tag Build is passing Details

master v1.0.1
李光春 2 years ago
parent 4ea955eacd
commit e12d5cc9fd

@ -1,5 +1,3 @@
module go.dtapp.net/goenv module go.dtapp.net/goenv
go 1.18 go 1.18
require go.dtapp.net/gostring v1.0.3

@ -1,2 +0,0 @@
go.dtapp.net/gostring v1.0.3 h1:KSOq4D77/g5yZN/bqWfZ0kOOaPr/P1240vg03+XdENI=
go.dtapp.net/gostring v1.0.3/go.mod h1:+ggrOvgQDQturi1QGsXEpyRN/ZPoRDaqhMujIk5lrgQ=

@ -1,8 +1,8 @@
package goenv package goenv
import ( import (
"go.dtapp.net/gostring"
"os" "os"
"strconv"
"strings" "strings"
) )
@ -17,7 +17,17 @@ func GetEnvDefault(key, defVal string) string {
func GetEnvDefaultInt(key string, defVal int) int { func GetEnvDefaultInt(key string, defVal int) int {
val, ok := os.LookupEnv(key) val, ok := os.LookupEnv(key)
if ok { if ok {
return gostring.ToInt(val) i, _ := strconv.Atoi(val)
return i
}
return defVal
}
func GetEnvDefaultBool(key string, defVal bool) bool {
val, ok := os.LookupEnv(key)
if ok {
b, _ := strconv.ParseBool(val)
return b
} }
return defVal return defVal
} }

@ -5,18 +5,28 @@ import "testing"
func TestGetEnvDefault(t *testing.T) { func TestGetEnvDefault(t *testing.T) {
t.Log(GetEnvDefault("test1", "1")) t.Log(GetEnvDefault("test1", "1"))
} }
func TestGetEnvDefaultInt(t *testing.T) {
t.Log(GetEnvDefaultInt("test2", 2))
}
func BenchmarkGetEnvDefault(b *testing.B) { func BenchmarkGetEnvDefault(b *testing.B) {
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
b.Log(GetEnvDefault("test1", "1")) b.Log(GetEnvDefault("test1", "1"))
} }
} }
func TestGetEnvDefaultInt(t *testing.T) {
t.Log(GetEnvDefaultInt("test2", 2))
}
func BenchmarkGetEnvDefaultInt(b *testing.B) { func BenchmarkGetEnvDefaultInt(b *testing.B) {
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
b.Log(GetEnvDefaultInt("test2", 2)) b.Log(GetEnvDefaultInt("test2", 2))
} }
} }
func TestGetEnvDefaultBool(t *testing.T) {
t.Log(GetEnvDefaultBool("test3", true))
}
func BenchmarkGetEnvDefaultBool(b *testing.B) {
for i := 0; i < b.N; i++ {
b.Log(GetEnvDefaultBool("test3", true))
}
}

@ -1,3 +1,3 @@
package goenv package goenv
const Version = "1.0.0" const Version = "1.0.1"

Loading…
Cancel
Save