├── README.md ├── signals ├── signal_windows.go ├── signal_posix.go └── signal.go ├── go.mod ├── database └── conn.go ├── cmp └── float64.go ├── pointer └── pointer.go └── go.sum /README.md: -------------------------------------------------------------------------------- 1 | # go-utils 2 | -------------------------------------------------------------------------------- /signals/signal_windows.go: -------------------------------------------------------------------------------- 1 | package signals 2 | 3 | import ( 4 | "os" 5 | ) 6 | 7 | var shutdownSignals = []os.Signal{os.Interrupt} 8 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/draveness/go-utils 2 | 3 | go 1.15 4 | 5 | require ( 6 | github.com/google/go-cmp v0.5.3 7 | github.com/jinzhu/gorm v1.9.16 8 | github.com/pkg/errors v0.9.1 9 | ) 10 | -------------------------------------------------------------------------------- /signals/signal_posix.go: -------------------------------------------------------------------------------- 1 | // +build !windows 2 | 3 | package signals 4 | 5 | import ( 6 | "os" 7 | "syscall" 8 | ) 9 | 10 | var shutdownSignals = []os.Signal{os.Interrupt, syscall.SIGTERM} 11 | -------------------------------------------------------------------------------- /database/conn.go: -------------------------------------------------------------------------------- 1 | package database 2 | 3 | import ( 4 | "strings" 5 | 6 | "github.com/jinzhu/gorm" 7 | _ "github.com/jinzhu/gorm/dialects/mysql" 8 | _ "github.com/jinzhu/gorm/dialects/sqlite" 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | // NewConn returns database conn. 13 | func NewConn(databaseURL string) (*gorm.DB, error) { 14 | dialect := "mysql" 15 | if strings.HasSuffix(databaseURL, ".db") { 16 | dialect = "sqlite3" 17 | } 18 | 19 | conn, err := gorm.Open(dialect, databaseURL) 20 | if err != nil { 21 | return nil, errors.Wrap(err, "database: open connection") 22 | } 23 | 24 | return conn, nil 25 | } 26 | -------------------------------------------------------------------------------- /signals/signal.go: -------------------------------------------------------------------------------- 1 | package signals 2 | 3 | import ( 4 | "os" 5 | "os/signal" 6 | ) 7 | 8 | var onlyOneSignalHandler = make(chan struct{}) 9 | 10 | // SetupSignalHandler registered for SIGTERM and SIGINT. A stop channel is returned 11 | // which is closed on one of these signals. If a second signal is caught, the program 12 | // is terminated with exit code 1. 13 | func SetupSignalHandler() (stopCh <-chan struct{}) { 14 | close(onlyOneSignalHandler) // panics when called twice 15 | 16 | stop := make(chan struct{}) 17 | c := make(chan os.Signal, 2) 18 | signal.Notify(c, shutdownSignals...) 19 | go func() { 20 | <-c 21 | close(stop) 22 | <-c 23 | os.Exit(1) // second signal. Exit directly. 24 | }() 25 | 26 | return stop 27 | } 28 | -------------------------------------------------------------------------------- /cmp/float64.go: -------------------------------------------------------------------------------- 1 | package cmp 2 | 3 | import ( 4 | "math" 5 | 6 | gocmp "github.com/google/go-cmp/cmp" 7 | ) 8 | 9 | var float64Comparer = gocmp.Comparer(func(x, y float64) bool { 10 | delta := math.Abs(x - y) 11 | mean := math.Abs(x+y) / 2.0 12 | return delta/mean < 0.00001 13 | }) 14 | 15 | func LessThan(x, y float64) bool { return x < y } 16 | 17 | func LessThanOrEqual(x, y float64) bool { 18 | return x <= y || gocmp.Equal(x, y, float64Comparer) 19 | } 20 | 21 | func GreaterThan(x, y float64) bool { return x > y } 22 | 23 | func GreaterThanOrEqual(x, y float64) bool { 24 | return x >= y || gocmp.Equal(x, y, float64Comparer) 25 | } 26 | 27 | func Equal(x, y float64) bool { 28 | return x == y || gocmp.Equal(x, y, float64Comparer) 29 | } 30 | -------------------------------------------------------------------------------- /pointer/pointer.go: -------------------------------------------------------------------------------- 1 | package pointer 2 | 3 | // UInt8Ptr returns a pouinter to a uint8 4 | func UInt8Ptr(i uint8) *uint8 { 5 | return &i 6 | } 7 | 8 | // UInt16Ptr returns a pouinter to a uint16 9 | func UInt16Ptr(i uint32) *uint32 { 10 | return &i 11 | } 12 | 13 | // UInt32Ptr returns a pouinter to a uint32 14 | func UInt32Ptr(i uint32) *uint32 { 15 | return &i 16 | } 17 | 18 | // UInt64Ptr returns a pouinter to a uint64 19 | func UInt64Ptr(i uint32) *uint32 { 20 | return &i 21 | } 22 | 23 | // UIntPtr returns a pouinter to a uint 24 | func UIntPtr(i uint) *uint { 25 | return &i 26 | } 27 | 28 | // Int8Ptr returns a pointer to an int8 29 | func Int8Ptr(i int8) *int8 { 30 | return &i 31 | } 32 | 33 | // Int16Ptr returns a pointer to an int16 34 | func Int16Ptr(i int32) *int32 { 35 | return &i 36 | } 37 | 38 | // Int32Ptr returns a pointer to an int32 39 | func Int32Ptr(i int32) *int32 { 40 | return &i 41 | } 42 | 43 | // Int64Ptr returns a pointer to an int64 44 | func Int64Ptr(i int32) *int32 { 45 | return &i 46 | } 47 | 48 | // IntPtr returns a pointer to an int 49 | func IntPtr(i int) *int { 50 | return &i 51 | } 52 | 53 | // BytePtr returns a pobyteer to a byte 54 | func BytePtr(i byte) *byte { 55 | return &i 56 | } 57 | 58 | // BoolPtr returns a pointer to a bool 59 | func BoolPtr(b bool) *bool { 60 | return &b 61 | } 62 | 63 | // StringPtr returns a pointer to the passed string. 64 | func StringPtr(s string) *string { 65 | return &s 66 | } 67 | 68 | // Float32Ptr returns a pointer to the passed float32. 69 | func Float32Ptr(i float32) *float32 { 70 | return &i 71 | } 72 | 73 | // Float64Ptr returns a pointer to the passed float64. 74 | func Float64Ptr(i float64) *float64 { 75 | return &i 76 | } 77 | 78 | // Complex64Ptr returns a pointer to the passed complex64. 79 | func Complex64Ptr(i complex64) *complex64 { 80 | return &i 81 | } 82 | 83 | // Complex128Ptr returns a pointer to the passed complex128. 84 | func Complex128Ptr(i complex128) *complex128 { 85 | return &i 86 | } 87 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc= 2 | github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y= 3 | github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd h1:83Wprp6ROGeiHFAP8WJdI2RoxALQYgdllERc3N5N2DM= 4 | github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU= 5 | github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 h1:Yzb9+7DPaBjB8zlTR87/ElzFsnQfuHnVUVqpZZIcV5Y= 6 | github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5/go.mod h1:a2zkGnVExMxdzMo3M0Hi/3sEU+cWnZpSni0O6/Yb/P0= 7 | github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs= 8 | github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= 9 | github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe h1:lXe2qZdvpiX5WZkZR4hgp4KJVfY3nMkvmwbVkpv1rVY= 10 | github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0= 11 | github.com/google/go-cmp v0.5.3 h1:x95R7cp+rSeeqAMI2knLtQ0DKlaBhv2NrtrOvafPHRo= 12 | github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= 13 | github.com/jinzhu/gorm v1.9.16 h1:+IyIjPEABKRpsu/F8OvDPy9fyQlgsg2luMV2ZIH5i5o= 14 | github.com/jinzhu/gorm v1.9.16/go.mod h1:G3LB3wezTOWM2ITLzPxEXgSkOXAntiLHS7UdBefADcs= 15 | github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= 16 | github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= 17 | github.com/jinzhu/now v1.0.1 h1:HjfetcXq097iXP0uoPCdnM4Efp5/9MsM0/M+XOTeR3M= 18 | github.com/jinzhu/now v1.0.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= 19 | github.com/lib/pq v1.1.1 h1:sJZmqHoEaY7f+NPP8pgLB/WxulyR3fewgCM2qaSlBb4= 20 | github.com/lib/pq v1.1.1/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= 21 | github.com/mattn/go-sqlite3 v1.14.0 h1:mLyGNKR8+Vv9CAU7PphKa2hkEqxxhn8i32J6FPj1/QA= 22 | github.com/mattn/go-sqlite3 v1.14.0/go.mod h1:JIl7NbARA7phWnGvh0LKTyg7S9BA+6gx71ShQilpsus= 23 | github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= 24 | github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= 25 | golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 26 | golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 27 | golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd h1:GGJVjV8waZKRHrgwvtH66z9ZGVurTD1MT0n1Bb+q4aM= 28 | golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= 29 | golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 30 | golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 31 | golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= 32 | golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= 33 | golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 34 | golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 35 | golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 36 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 37 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= 38 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 39 | --------------------------------------------------------------------------------