DEV Community

Cover image for gookit/goutil - Useful utils package for the Go
Inhere
Inhere

Posted on

gookit/goutil - Useful utils package for the Go

gookit/goutil 💪 Useful utils package for the Go: int, string, array/slice, map, error, time, format, CLI, ENV, filesystem, system, testing and more.

Sub-packages

  • arrutil: Array/Slice util functions. eg: check, convert, formatting
  • dump: Simple variable printing tool, printing slice, map will automatically wrap each element and display the call location
  • cliutil Command-line util functions. eg: read input, exec command, cmdline parse/build
  • errorx Provide an enhanced error implements for go, allow with stacktraces and wrap another error.
  • envutil ENV util for current runtime env information. eg: get one, get info, parse var
  • fmtutil Format data util functions. eg: data, size
  • fsutil Filesystem util functions, quick create, read and write file. eg: file and dir check, operate
  • jsonutil some util functions for quick read, write, encode, decode JSON data.
  • maputil Map data util functions. eg: convert, sub-value get, simple merge
  • mathutilMath(int, number) util functions. eg: convert, math calc, random
  • netutil Network util functions
    • netutil/httpreq An easier-to-use HTTP client that wraps http.Client
  • strutil String util functions. eg: bytes, check, convert, encode, format and more
  • sysutil System util functions. eg: sysenv, exec, user, process
  • testutil Test help util functions. eg: http test, mock ENV value
  • timex Provides an enhanced time.Time implementation. Add more commonly used functional methods
    • such as: DayStart(), DayAfter(), DayAgo(), DateFormat() and more.

Git repository

Install

go get github.com/gookit/goutil
Enter fullscreen mode Exit fullscreen mode

Usage

goutil/dump

goutil/dump is a golang data printing toolkit that prints beautiful and easy to read go slice, map, struct data.

Examples

//  go run ./dump/_examples/struct.go
func main() {
    s1 := &struct {
        cannotExport map[string]interface{}
    }{
        cannotExport: map[string]interface{}{
            "key1": 12,
            "key2": "abcd123",
        },
    }

    s2 := struct {
        ab string
        Cd int
    }{
        "ab", 23,
    }

    color.Infoln("- Use fmt.Println:")
    fmt.Println(s1, s2)

    color.Infoln("\n- Use dump.Println:")
    dump.P(s1,s2)
}
Enter fullscreen mode Exit fullscreen mode

Output:

image

goutil/timex

goutil/timex Provides an enhanced time.Time implementation, and add more commonly used functional methods.

such as: DayStart(), DayAfter(), DayAgo(), DateFormat() and more

Create timex instance:

now := timex.Now()

// from time.Time
tx := timex.New(time.Now())
tx := timex.FromTime(time.Now())

// from time unix
tx := timex.FromUnix(1647411580)
Enter fullscreen mode Exit fullscreen mode

Create time for date string:

// auto match layout by datetime
tx, err  := timex.FromString("2022-04-20 19:40:34")
// custom set the datetime layout
tx, err  := timex.FromString("2022-04-20 19:40:34", "2006-01-02 15:04:05")
// use date template as layout
tx, err  := timex.FromDate("2022-04-20 19:40:34", "Y-m-d H:I:S")
Enter fullscreen mode Exit fullscreen mode

Quickly fetch time:

tx := timex.Now()

tx.Yesterday()
tx.Tomorrow()

tx.DayStart() // get time at Y-m-d 00:00:00
tx.DayEnd() // get time at Y-m-d 23:59:59
tx.HourStart() // get time at Y-m-d H:00:00
tx.HourEnd() // get time at Y-m-d H:59:59

tx.AddDay(2)
tx.AddHour(1)
tx.AddMinutes(15)
tx.AddSeconds(120)
Enter fullscreen mode Exit fullscreen mode

Quickly format date:

tx.DateFormat("y/m/d H:I")
date := tx.DateFormat("Y-m-d H:I:S") // Output: 2022-04-20 19:40:34
date = tx.DateFormat("y-m-d H:I:S") // Output: 22-04-20 19:40:34
Enter fullscreen mode Exit fullscreen mode

More usage

More utils and usage please see README https://github.com/gookit/goutil/blob/master/README.md

Top comments (0)