From e12d5cc9fd646601b2a256be3d1bc3e59fc3600a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=85=89=E6=98=A5?= Date: Tue, 14 Jun 2022 00:25:46 +0800 Subject: [PATCH] - update --- go.mod | 2 -- go.sum | 2 -- goenv.go | 14 ++++++++++++-- goenv_test.go | 18 ++++++++++++++---- version.go | 2 +- 5 files changed, 27 insertions(+), 11 deletions(-) diff --git a/go.mod b/go.mod index 1d0c370..5096b20 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,3 @@ module go.dtapp.net/goenv go 1.18 - -require go.dtapp.net/gostring v1.0.3 diff --git a/go.sum b/go.sum index 5d8b1b3..e69de29 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/goenv.go b/goenv.go index cca1347..cce7ce4 100644 --- a/goenv.go +++ b/goenv.go @@ -1,8 +1,8 @@ package goenv import ( - "go.dtapp.net/gostring" "os" + "strconv" "strings" ) @@ -17,7 +17,17 @@ func GetEnvDefault(key, defVal string) string { func GetEnvDefaultInt(key string, defVal int) int { val, ok := os.LookupEnv(key) 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 } diff --git a/goenv_test.go b/goenv_test.go index 6f599fa..fc234d0 100644 --- a/goenv_test.go +++ b/goenv_test.go @@ -5,18 +5,28 @@ import "testing" func TestGetEnvDefault(t *testing.T) { t.Log(GetEnvDefault("test1", "1")) } -func TestGetEnvDefaultInt(t *testing.T) { - t.Log(GetEnvDefaultInt("test2", 2)) -} - func BenchmarkGetEnvDefault(b *testing.B) { for i := 0; i < b.N; i++ { b.Log(GetEnvDefault("test1", "1")) } } +func TestGetEnvDefaultInt(t *testing.T) { + t.Log(GetEnvDefaultInt("test2", 2)) +} + func BenchmarkGetEnvDefaultInt(b *testing.B) { for i := 0; i < b.N; i++ { 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)) + } +} diff --git a/version.go b/version.go index 628cec4..a4cb1b9 100644 --- a/version.go +++ b/version.go @@ -1,3 +1,3 @@ package goenv -const Version = "1.0.0" +const Version = "1.0.1"