Documentation ¶
Index ¶
- Constants
- func AwaitAll[Input any, Response any](inputs []Input, asyncFunc func(Input) Response) []Response
- func AwaitAllWRL[Input any, Response any](inputs []Input, asyncFunc func(Input) Response, concurrency int) []Response
- func BinarySearch(nums []int, target int) bool
- func BubbleSort(nums []int) []int
- func BucketSort(nums []int, bucketSize int) []int
- func BuildQuery(m map[string]interface{}) (q string)
- func ClimbStairs3(x int) int
- func Contains(slice interface{}, item interface{}) bool
- func CountWeekday(start, end time.Time, day time.Weekday) int
- func CsvWrite(filename string, records [][]string) (err error)
- func DayBeginAt(t time.Time) int64
- func DayEndedAt(t time.Time) int64
- func Delete(url string, rq []byte, headers map[string]string, timeout time.Duration) (rp []byte, err error)
- func FibonacciDP(n int) int
- func FibonacciRecursion(n int) int
- func FileCreate(filename string) (*os.File, error)
- func FileExists(filename string) (y bool, err error)
- func Get(url string, params map[string]interface{}, headers map[string]string, ...) (ret []byte, err error)
- func GinReverseProxy(c *gin.Context, remoteUrl string) error
- func GinRun(host, port string) error
- func IdMask(id int64) (str string)
- func IdUnmask(mask string) (id int64)
- func InsertionSort(arr []int)
- func IsAscii(s string) bool
- func IsJsonEmpty(data []byte) bool
- func IsNumber(x interface{}) (y bool)
- func IsStrEmpty(str string) bool
- func MD5(str string) string
- func PathClean(dir string) (err error)
- func Post(url string, rq []byte, headers map[string]string, timeout time.Duration) (rp []byte, err error)
- func Put(url string, rq []byte, headers map[string]string, timeout time.Duration) (rp []byte, err error)
- func QuickSort(nums []int) []int
- func Rand() *rand.Rand
- func RandStr(n int) string
- func Retry(attempts int, sleep time.Duration, f func() error) error
- func SliceIntDiff2Right(a, b []int) (c []int)
- func SliceRandOne(sl interface{}) (item interface{})
- func Struct2Map(in interface{}) (out map[string]interface{}, err error)
- func Struct2MapMarshal(st interface{}) (m map[string]interface{}, err error)
- func Struct2Str(st interface{}, tag, sep string) (s string, err error)
- func Struct2StrAndWrap(st interface{}, tag, sep, wrap string) (s string, err error)
- func Time2now(t time.Time) string
- func Time2short(d time.Duration) string
- func TimeDiffDays(first time.Time, second time.Time) int
- func TimeIsLeapYear(t time.Time) bool
- func TimeIsSameDay(first time.Time, second time.Time) bool
- func ToInterfaceSlice(sl interface{}) (isl []interface{})
- func Token() string
- func Unique(slicePtr interface{})
- func Uuid22() string
- func Uuid32() string
- func UuidSimple() string
- func WriteBin(filename string, data interface{}) (err error)
- type Err
- type RoundRobinScheduler
- type RrItem
Examples ¶
- BinarySearch
- BubbleSort
- BucketSort
- ClimbStairs3
- CountWeekday
- DayBeginAt
- DayEndedAt
- Err.Error
- FibonacciRecursion
- Get
- GinReverseProxy
- IdUnmask
- InsertionSort
- IsNumber
- MD5
- QuickSort
- SliceIntDiff2Right
- Struct2Map
- Struct2Str
- Struct2StrAndWrap
- Time2now
- Time2short
- TimeDiffDays
- TimeIsLeapYear
- TimeIsSameDay
- ToInterfaceSlice
Constants ¶
const ErrCodeApi = 3001
const ErrCodeAuth = 1003
const ErrCodeCommon = 1001
const ErrCodeDb = 2001
const ErrCodeMySQL = 2003
const ErrCodeParam = 1002
const ErrCodeRedis = 2002
Variables ¶
This section is empty.
Functions ¶
func AwaitAllWRL ¶
func AwaitAllWRL[Input any, Response any](inputs []Input, asyncFunc func(Input) Response, concurrency int) []Response
AwaitAllWRL AwaitAll with Rate Limit
func BinarySearch ¶
Example ¶
nums := []int{22, 11, 33, 100} target1 := 1 target2 := 11 i1 := BinarySearch(nums, target1) i2 := BinarySearch(nums, target2) fmt.Println(i1, i2)
Output: false true
func BubbleSort ¶
BubbleSort The time complexity of bubble sorting is O(n^2), which is less efficient
Example ¶
nums := []int{22, 11, 33, 55, 44, 66} nums2 := BubbleSort(nums) fmt.Println(nums2)
Output: [11 22 33 44 55 66]
func BucketSort ¶
Example ¶
nums := []int{22, 11, 33, 55, 44, 66, 7} nums2 := BucketSort(nums, 2) fmt.Println(nums2)
Output: [7 11 22 33 44 55 66]
func ClimbStairs3 ¶
ClimbStairs3 chatgpt generated code Method: ClimbStairs3 Language: Go Description: When climbing stairs, you can walk one, two or three steps at a time. How many ways are there in total? Parameters: This method receives the following parameters: x: total number of steps Output: Number of ways to climb the stairs
Example ¶
n := ClimbStairs3(22) fmt.Println(n)
Output: 410744
func CountWeekday ¶
Example ¶
c1 := CountWeekday(time.Unix(time.Now().Unix()-86400*14, 0), time.Now(), time.Tuesday) fmt.Println(c1)
Output: 2
func DayBeginAt ¶
DayBeginAt unix timestamp of zero o'clock of the day
Example ¶
1693193334 2023-08-28 11:28:54 1693152000 2023-08-28 00:00:00
t := time.Unix(1693193334, 0) u := DayBeginAt(t) fmt.Println(time.Unix(u, 0).Format(time.TimeOnly))
Output: 00:00:00
func DayEndedAt ¶
DayEndedAt unix timestamp of 23:59:59 o'clock of the day
Example ¶
tm := time.Unix(1693193334, 0) tme := DayEndedAt(tm) fmt.Println(time.Unix(tme, 0).Format(time.TimeOnly))
Output: 23:59:59
func Delete ¶
func Delete(url string, rq []byte, headers map[string]string, timeout time.Duration) (rp []byte, err error)
Delete HTTP DELETE
func FibonacciDP ¶
func FibonacciRecursion ¶
Example ¶
n1 := FibonacciRecursion(22) n2 := FibonacciDP(22) fmt.Println(n1, n1 == n2)
Output: 17711 true
func FileExists ¶
func Get ¶
func Get(url string, params map[string]interface{}, headers map[string]string, timeout time.Duration) (ret []byte, err error)
Get Http Get query array: convert map["name1"]["v1", "v2"] to name1[]=v1&name1[]=v2
Example ¶
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, "Hello, Get") })) defer ts.Close() res, err := Get(ts.URL, nil, nil, 10*time.Second) if err != nil { log.Fatal(err) } fmt.Printf("%s", string(res))
Output: Hello, Get
func GinReverseProxy ¶
GinReverseProxy gin reverse proxy
Example ¶
engine := gin.New() addr := fmt.Sprintf("0.0.0.0:8080") engine.Any("/reverseProxy/:proxyPath", func(c *gin.Context) { remoteUrl := fmt.Sprintf("https://remoteServer.com/%s", c.Param("proxyPath")) err := GinReverseProxy(c, remoteUrl) if err != nil { log.Fatal(err) } }) err := engine.Run(addr) if err != nil { log.Fatal(err) }
Output:
func IdUnmask ¶
Example ¶
package main import ( "fmt" "github.com/lleiiell/goist" ) func main() { fmt.Println(goist.IdUnmask("1clb")) fmt.Println(goist.IdUnmask("rr5fk")) fmt.Println(goist.IdUnmask("1djlwzxe5tj1a4qw")) }
Output: 1 999 6521156348675224222
func InsertionSort ¶
func InsertionSort(arr []int)
Example ¶
nums := []int{22, 11, 33, 55, 44, 66} fmt.Println(nums) InsertionSort(nums) fmt.Println(nums)
Output: [22 11 33 55 44 66] [11 22 33 44 55 66]
func IsJsonEmpty ¶
func IsNumber ¶
func IsNumber(x interface{}) (y bool)
Example ¶
fmt.Println(IsNumber("a"), IsNumber([]interface{}{"a", 1})) fmt.Println(IsNumber(1), IsNumber(-1), IsNumber(1.23), IsNumber(complex(1, 2)), IsNumber(1+2i))
Output: false false true true true true true
func IsStrEmpty ¶
func MD5 ¶
Example ¶
package main import ( "fmt" "github.com/lleiiell/goist" ) func main() { fmt.Println(goist.MD5("hello")) }
Output: 5d41402abc4b2a76b9719d911017c592
func Post ¶
func Post(url string, rq []byte, headers map[string]string, timeout time.Duration) (rp []byte, err error)
Post HTTP POST
func Put ¶
func Put(url string, rq []byte, headers map[string]string, timeout time.Duration) (rp []byte, err error)
Put HTTP PUT
func QuickSort ¶
QuickSort The efficiency of the quicksort algorithm is related to the choice of pivot. If the pivot selected each time is exactly the median of the array, then the time complexity of the algorithm is O(n log n). If the pivot selected each time happens to be the minimum or maximum value of the array, then the time complexity of the algorithm is O(n^2), and the efficiency is low.
Example ¶
nums := []int{22, 11, 33, 55, 44, 66} nums2 := QuickSort(nums) fmt.Println(nums2)
Output: [11 22 33 44 55 66]
func SliceIntDiff2Right ¶
SliceIntDiff2Right returns the elements in `a` that aren't in `b`.
Example ¶
s1 := []int{1, 2, 3} s2 := []int{2, 4, 5} fmt.Println(SliceIntDiff2Right(s1, s2))
Output: [1 3]
func SliceRandOne ¶
func SliceRandOne(sl interface{}) (item interface{})
func Struct2Map ¶
Example ¶
st := struct { Name string `json:"name,omitempty"` True bool `json:"true"` }{ "till", true, } // m, err := Struct2Map(st) // fmt.Println(m["name"], m["true"], err) // Output: till true <nil> fmt.Println(Struct2Map(st))
Output: map[name:till true:true] <nil>
func Struct2MapMarshal ¶
func Struct2Str ¶
Example ¶
st := struct { Name string `json:"name,omitempty"` True bool `json:"true"` Title string }{ "till", true, "title", } // m, err := Struct2Map(st) // fmt.Println(m["name"], m["true"], err) // Output: till true <nil> fmt.Println(Struct2Str(st, "", ""))
Output: name,true,Title <nil>
func Struct2StrAndWrap ¶
Example ¶
st := struct { Name string `json:"name,omitempty"` True bool `json:"true"` Title string }{ "till", true, "title", } fmt.Println(Struct2StrAndWrap(st, "", "", "`"))
Output: `name`,`true`,`Title` <nil>
func Time2now ¶
Example ¶
fmt.Println(Time2now(time.Unix(time.Now().Unix()-5, 0))) fmt.Println(Time2now(time.Unix(time.Now().Unix()-60*2-5, 0))) fmt.Println(Time2now(time.Unix(time.Now().Unix()-3600*2-5, 0))) fmt.Println(Time2now(time.Unix(time.Now().Unix()-86400*2-5, 0)))
Output: 刚刚 2分钟 2小时 2天
func Time2short ¶
Time2short short string format
Example ¶
fmt.Println(Time2short(10236 * time.Millisecond)) fmt.Println(Time2short(1023600 * time.Millisecond)) fmt.Println(Time2short(10236000 * time.Millisecond))
Output: 10.24s 17.06m 2.84h
func TimeDiffDays ¶
TimeDiffDays number of days between given time
Example ¶
t0, _ := time.Parse(time.DateTime, "2023-08-28 00:10:00") t1, _ := time.Parse(time.DateTime, "2023-08-28 23:00:00") t2, _ := time.Parse(time.DateTime, "2023-08-29 00:10:00") t3, _ := time.Parse(time.DateTime, "2023-08-29 23:59:59") t4, _ := time.Parse(time.DateTime, "2022-08-29 23:59:59") fmt.Println(TimeDiffDays(t1, t0), TimeDiffDays(t2, t0), TimeDiffDays(t3, t1), TimeDiffDays(t4, t0))
Output: 0 1 1 364
func TimeIsLeapYear ¶
Example ¶
t1, _ := time.Parse("2006-01-02 15", "2000-09-01 1") t2, _ := time.Parse("2006-01-02 15", "1900-09-03 23") t3, _ := time.Parse("2006-01-02 15", "2024-08-28 23") fmt.Println(TimeIsLeapYear(t1), TimeIsLeapYear(t2), TimeIsLeapYear(t3))
Output: true false true
func TimeIsSameDay ¶
Example ¶
t0, _ := time.Parse(time.DateTime, "2023-08-28 00:10:00") t1, _ := time.Parse(time.DateTime, "2023-08-28 23:00:00") t2, _ := time.Parse(time.DateTime, "2023-08-29 00:10:00") t3, _ := time.Parse(time.DateTime, "2023-08-29 23:59:59") t4, _ := time.Parse(time.DateTime, "2022-08-29 23:59:59") fmt.Println(TimeIsSameDay(t0, t1), TimeIsSameDay(t0, t2), TimeIsSameDay(t3, t4))
Output: true false false
func ToInterfaceSlice ¶
func ToInterfaceSlice(sl interface{}) (isl []interface{})
Example ¶
s1 := []int{1, 2, 3} is1 := ToInterfaceSlice(s1) fmt.Println(reflect.ValueOf(is1).Type(), reflect.ValueOf(s1).Type()) sl2 := []string{"a", "b", "c"} isl2 := ToInterfaceSlice(sl2) fmt.Println(reflect.ValueOf(isl2).Type(), reflect.ValueOf(sl2).Type())
Output: []interface {} []int []interface {} []string
Types ¶
type RoundRobinScheduler ¶
type RoundRobinScheduler struct {
// contains filtered or unexported fields
}
func (*RoundRobinScheduler) Add ¶
func (r *RoundRobinScheduler) Add(item RrItem)
func (*RoundRobinScheduler) Next ¶
func (r *RoundRobinScheduler) Next() RrItem
func (*RoundRobinScheduler) Remove ¶
func (r *RoundRobinScheduler) Remove(item RrItem)