Skip to content

whitekid/goxp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go utility function collection

Go

need more detailed usage? please refer test cases.

goroutines

DoWithWorker() iterate chan and do with workers
Every() run func with interval
After() run function with delay
Async(),Async2() run function with goroutine and get result asynchronous

JSON/ XML/ YAML IO

ReadJSON() decode json to type
WriteJSON() encode json towriter
ReadXML() decode xml to type
WriteXML() encode json towriter
ReadYAML() decode yaml to type
WriteYAML() encode json towriter

misc

SetBits()
ClearBits()
IsContextDone() return true if context is done
WithTimeout() run fn() with timeout context
Must(error) panic if error not nil
NewPool[T]() sync.Pool with generics
Abs()

string parse with defaults

AtoiDef() strconv.Atoi() with default value
ParseBoolDef() strconv.ParseBool() with default value
ParseIntDef() strconv.ParseInt() with default value

Parse time and format

ParseDateTime() parse string to time for well known layouts
TimeWithLayout time.Time with layouts
RFC1123ZTime Mon, 02 Jan 2006 15:04:05 -0700
RFC3339Time 2006-01-02T15:04:05Z07:00

Shell execution

Exec() - simple run command

run command and output to stdin/stdout

exc := Exec("ls", "-al")
err := exc.Do(context.Background())
require.NoError(t, err)

run command and get output

exc := Exec("ls", "-al")
output, err := exc.Output(context.Background())
require.NoError(t, err)
require.Contains(t, string(output), "README.md")

Pipe() - run command with pipe

Conditional execution

If(), Else(), IfThen() - run func as condition

IfThen(true, func() { fmt.Printf("true\n") })
// true

IfThen(true, func() { fmt.Printf("true\n") }, func() { fmt.Printf("false\n") })
// true

IfThen(false, func() { fmt.Printf("true\n") }, func() { fmt.Printf("false\n") })
// false

IfThen(false, func() { fmt.Printf("true\n") }, func() { fmt.Printf("false\n") }, func() { fmt.Printf("false\n") })
// false

play

Ternary(), TernaryF(), TernaryCF()

Random string/ byte generator

RandomByte() - generate random byte

b := RandomByte(10)
// hex.EncodeToString(b) = "4d46ef2f87b8191daf58"

RandomString(), RandomStringWith() - generate random string

s := RandomString(10)
// s = "$c&I$#LR3Y"

s := RandomStringWith(10, []rune("abcdefg"))
// s = "bbffedabda"

Timer() - measure execution time

func doSomething() {
    defer goxp.Timer("doSomething()")()
    time.Sleep(500 * time.Millisecond)
}

doSomething()
// time takes 500.505063ms: doSomething()

play

Tuple

Tuple2, Tuple3 Pack() and Unpack()
T2,T3 construct tuple with element

sub packages

  • chanx - chan extensions
  • cobrax - cobra and viper utility functions
  • cryptox - encrypt/ decrypt functions
  • errors - errors with stack tracd
  • fixtures - useful fixture functions for test
  • flags - cobra & viper make easy
  • fx - experimental: some functional functions by iter.Seq
  • fx/gen - experimental: generator
  • httptest - test http sever make easy
  • iterx - iter.Seq extensions
  • log - simple log powered by zap
  • mapx - map extesions
  • requests - simple http client
  • retry - retrier with backoff
  • services - simple service framework
  • sets - Set type
  • slicex - slice extensions
  • slug - uuid to slug
  • testx - unit test utility functions
  • validate - validator make easy
  • x509x - x509 utility functions

About

go utility functions for simple life

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •