From 8e1d760a7c1a2de60ef569837411bd7468743627 Mon Sep 17 00:00:00 2001 From: ieee0824 Date: Fri, 18 Aug 2017 15:43:58 +0900 Subject: [PATCH] =?UTF-8?q?int=E7=B3=BB=E3=81=AE=E5=9E=8B=E3=81=AEparser?= =?UTF-8?q?=E3=82=92=E5=A2=97=E3=82=84=E3=81=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- int.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/int.go b/int.go index 0657e45..9407373 100644 --- a/int.go +++ b/int.go @@ -3,6 +3,7 @@ package getenv import ( "os" "strconv" + "log" ) func Int(key string, def ...int) int { @@ -20,3 +21,54 @@ func Int(key string, def ...int) int { } return i } + +func Int32(key string, def ...int32) (int32) { + var d int32 + if len(def) != 0 { + d = def[0] + } + v := os.Getenv(key) + if v == "" { + return d + } + i32, err := strconv.ParseInt(v, 10, 32) + if err != nil { + log.Printf("parse error: input: %v, %v\n", v, err.Error()) + return d + } + return int32(i32) +} + +func Int64(key string, def ...int64) (int64) { + var d int64 + if len(def) != 0 { + d = def[0] + } + v := os.Getenv(key) + if v == "" { + return d + } + i64, err := strconv.ParseInt(v, 10, 64) + if err != nil { + log.Printf("parse error: input: %v, %v\n", v, err.Error()) + return d + } + return int64(i64) +} + +func Int16(key string, def ...int16) (int16) { + var d int16 + if len(def) != 0 { + d = def[0] + } + v := os.Getenv(key) + if v == "" { + return d + } + i16, err := strconv.ParseInt(v, 10, 16) + if err != nil { + log.Printf("parse error: input: %v, %v\n", v, err.Error()) + return d + } + return int16(i16) +} \ No newline at end of file