Skip to content

whitekid/goxp

Repository files navigation

go utility function collection

Go

need more detailed usage? please refer test cases.

may be not classfied yet..

  • After() - run func after some duration
  • AvailablePort() - return random available tcp ports
  • AvailableUdpPort() - return random available udp ports
  • ClearBit() - clear bit postion
  • DoWithWorker() - run go routine with n works
  • Every() - run goroutine in every duration
  • FileExists() - return true if file exists
  • Filename() - return current source file name
  • IsContextDone() - return true if context is done
  • JsonRecode() - redecode as new type
  • NewPool() - sync.Pool with type
  • SetBit() - set bit position
  • SetNX() - acts as redis SetNX
  • StrToTime() - parse standard time format as easy
  • URLToListenAddr() - parse url and get listenable address, ports

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")

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

Random string/ byte generator

RandomByte() - generate random byte

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

RandomString() - generate random string

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

RandomStringWith() - generate random string

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

RandomStringWithCrypto() - generate random string with crpto.rand

s := RandomStringWithCrypto(10)
// s = "d0tu0r3)oZ"

Timer() - measure execution time

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

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

play

sub packages

  • cryptox - encrypt/ decrypt functions
  • fixtures - useful fixture functions for test
  • flags - cobra & viper make easy
  • fx - experimental: some functional functions
  • httptest - test http sever make easy
  • log - simple log powered by zap
  • request - simple http client
  • retry - retrier with backoff
  • service - simple service framework
  • slug - uuid to slug
  • types - Some useful types
  • validator - validator make easy

About

go utility functions for simple life

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published