Skip to content

Commit c0967c2

Browse files
authored
Merge pull request #7 from ieee0824/int
int系の型のparserを増やす
2 parents 274d740 + 8e1d760 commit c0967c2

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

int.go

+52
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package getenv
33
import (
44
"os"
55
"strconv"
6+
"log"
67
)
78

89
func Int(key string, def ...int) int {
@@ -20,3 +21,54 @@ func Int(key string, def ...int) int {
2021
}
2122
return i
2223
}
24+
25+
func Int32(key string, def ...int32) (int32) {
26+
var d int32
27+
if len(def) != 0 {
28+
d = def[0]
29+
}
30+
v := os.Getenv(key)
31+
if v == "" {
32+
return d
33+
}
34+
i32, err := strconv.ParseInt(v, 10, 32)
35+
if err != nil {
36+
log.Printf("parse error: input: %v, %v\n", v, err.Error())
37+
return d
38+
}
39+
return int32(i32)
40+
}
41+
42+
func Int64(key string, def ...int64) (int64) {
43+
var d int64
44+
if len(def) != 0 {
45+
d = def[0]
46+
}
47+
v := os.Getenv(key)
48+
if v == "" {
49+
return d
50+
}
51+
i64, err := strconv.ParseInt(v, 10, 64)
52+
if err != nil {
53+
log.Printf("parse error: input: %v, %v\n", v, err.Error())
54+
return d
55+
}
56+
return int64(i64)
57+
}
58+
59+
func Int16(key string, def ...int16) (int16) {
60+
var d int16
61+
if len(def) != 0 {
62+
d = def[0]
63+
}
64+
v := os.Getenv(key)
65+
if v == "" {
66+
return d
67+
}
68+
i16, err := strconv.ParseInt(v, 10, 16)
69+
if err != nil {
70+
log.Printf("parse error: input: %v, %v\n", v, err.Error())
71+
return d
72+
}
73+
return int16(i16)
74+
}

0 commit comments

Comments
 (0)