github.com/leg100/ots@v0.0.7-0.20210919080622-034055ced4bd/ots.go (about) 1 /* 2 Package ots is responsible for domain logic. 3 */ 4 package ots 5 6 import ( 7 "fmt" 8 "math/rand" 9 "time" 10 ) 11 12 const ( 13 DefaultPageNumber = 1 14 DefaultPageSize = 20 15 MaxPageSize = 100 16 17 DefaultUserID = "user-123" 18 DefaultUsername = "ots" 19 20 alphanumeric = "abcdefghijkmnopqrstuvwxyzABCDEFGHIJKMNOPQRSTUVWXYZ0123456789" 21 ) 22 23 func String(str string) *string { return &str } 24 func Int(i int) *int { return &i } 25 func Int64(i int64) *int64 { return &i } 26 func UInt(i uint) *uint { return &i } 27 28 // TimeNow is a convenience func to return the pointer of the current time 29 func TimeNow() *time.Time { 30 t := time.Now() 31 return &t 32 } 33 34 // GenerateID generates a unique identifier with the given prefix 35 func GenerateID(prefix string) string { 36 return fmt.Sprintf("%s-%s", prefix, GenerateRandomString(16)) 37 } 38 39 // GenerateRandomString generates a random string composed of alphanumeric 40 // characters of length size. 41 func GenerateRandomString(size int) string { 42 // Without this, Go would generate the same random sequence each run. 43 rand.Seed(time.Now().UnixNano()) 44 45 buf := make([]byte, size) 46 for i := 0; i < size; i++ { 47 buf[i] = alphanumeric[rand.Intn(len(alphanumeric))] 48 } 49 return string(buf) 50 }