initial code (#6)

* initial code commit
This commit is contained in:
l3uddz
2021-02-14 16:18:26 +00:00
committed by GitHub
parent 3f55336fbd
commit ce3807b819
53 changed files with 3694 additions and 0 deletions

32
util/misc.go Normal file
View File

@@ -0,0 +1,32 @@
package util
import (
"crypto/sha256"
"fmt"
"strconv"
"strings"
)
func Atoi(val string, defaultVal int) int {
n, err := strconv.Atoi(val)
if err != nil {
return defaultVal
}
return n
}
func Atof64(val string, defaultVal float64) float64 {
n, err := strconv.ParseFloat(strings.TrimSpace(val), 64)
if err != nil {
return defaultVal
}
return n
}
func AsSHA256(o interface{}) string {
// credits: https://blog.8bitzen.com/posts/22-08-2019-how-to-hash-a-struct-in-go
h := sha256.New()
h.Write([]byte(fmt.Sprintf("%v", o)))
return fmt.Sprintf("%x", h.Sum(nil))
}

24
util/url.go Normal file
View File

@@ -0,0 +1,24 @@
package util
import (
"fmt"
"net/url"
"path"
"strings"
)
func JoinURL(base string, paths ...string) string {
// credits: https://stackoverflow.com/a/57220413
p := path.Join(paths...)
return fmt.Sprintf("%s/%s", strings.TrimRight(base, "/"), strings.TrimLeft(p, "/"))
}
func URLWithQuery(base string, q url.Values) (string, error) {
u, err := url.Parse(base)
if err != nil {
return "", fmt.Errorf("url parse: %w", err)
}
u.RawQuery = q.Encode()
return u.String(), nil
}