-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
122 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,33 @@ | ||
/* | ||
Common utilities | ||
*/ | ||
package serverutils | ||
|
||
import ( | ||
"log" | ||
"os" | ||
"strings" | ||
) | ||
|
||
var Time timeUtil | ||
var Random randomUtil | ||
|
||
// Return environment value or default value if not exists | ||
func GetEnvOrDefault(key string, defaultValue string) string { | ||
value := os.Getenv(key) | ||
if len(value) == 0 { | ||
value = defaultValue | ||
} | ||
return strings.TrimSpace(value) | ||
} | ||
|
||
func GetHostName() string { | ||
hostName, _ := os.Hostname() | ||
return hostName | ||
} | ||
|
||
func PrintEnv() { | ||
for _, env := range os.Environ() { | ||
log.Printf("Environment: %v", env) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package serverutils | ||
|
||
import ( | ||
"math/rand" | ||
"time" | ||
) | ||
|
||
const capitalLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | ||
const normalLetters = "abcdefghijklmnopqrstuvwxyz" | ||
const allLetters = normalLetters + capitalLetters | ||
|
||
var seed int64 = time.Now().UnixNano() | ||
|
||
type randomUtil struct { | ||
} | ||
|
||
func init() { | ||
rand.Seed(seed) | ||
} | ||
|
||
func (*randomUtil) GetRandomString(n int) string { | ||
b := make([]byte, n) | ||
for i := range b { | ||
b[i] = allLetters[rand.Intn(len(allLetters))] | ||
} | ||
return string(b) | ||
} | ||
|
||
func (*randomUtil) GetRandomStringFromString(n int, letters string) string { | ||
|
||
b := make([]byte, n) | ||
for i := range b { | ||
b[i] = letters[rand.Intn(len(letters))] | ||
} | ||
return string(b) | ||
} | ||
|
||
func (*randomUtil) GetRandomStringCapitalize(n int) string { | ||
|
||
b := make([]byte, n) | ||
for i := range b { | ||
b[i] = capitalLetters[rand.Intn(len(capitalLetters))] | ||
} | ||
return string(b) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters