goutil

package module
v0.0.0-...-754e97a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 18, 2020 License: MIT Imports: 8 Imported by: 0

README

Go Util

GitHub go.mod Go version GitHub tag (latest SemVer) GoDoc Go Report Card Build Status Coverage Status

Some utils for the Go: string, array/slice, map, format, cli, env, filesystem, test and more

中文说明

  • arrutil array/slice util
  • dump Simple variable printing tool, printing slice, map will automatically wrap each element and display the call location
  • cliutil CLI util
  • envutil ENV util for check current runtime env information
  • fmtutil format data tool
  • fsutil filesystem util
  • jsonutil JSON util
  • maputil map util
  • mathutil math util
  • netutil network util
  • strutil string util
  • testutil test help util

GoDoc

Packages

Array/Slice

package github.com/gookit/goutil/arrutil

func Reverse(ss []string)
func StringsRemove(ss []string, s string) []string
func StringsToInts(ss []string) (ints []int, err error)
func TrimStrings(ss []string, cutSet ...string) (ns []string)
CLI Util

package github.com/gookit/goutil/cliutil

func CurrentShell(onlyName bool) (path string)
func ExecCmd(binName string, args []string, workDir ...string) (string, error)
func ExecCommand(binName string, args []string, workDir ...string) (string, error)
func HasShellEnv(shell string) bool
func QuickExec(cmdLine string, workDir ...string) (string, error)
func ShellExec(cmdLine string, shells ...string) (string, error)
Dump Util

package github.com/gookit/goutil/dump

Simple variable printing tool, printing slice, map will automatically wrap each element and display the call location

func P(vs ...interface{})
func V(vs ...interface{})
func Print(vs ...interface{})

Usage please see dump/README.md

ENV Util

package github.com/gookit/goutil/envutil

func Getenv(name string, def ...string) string
func HasShellEnv(shell string) bool
func IsConsole(out io.Writer) bool
func IsLinux() bool
func IsMSys() bool
func IsMac() bool
func IsSupport256Color() bool
func IsSupportColor() bool
func IsWin() bool
func ParseEnvValue(val string) (newVal string)
Format Util

package github.com/gookit/goutil/fmtutil

func DataSize(bytes uint64) string
func HowLongAgo(sec int64) string
func PrettyJSON(v interface{}) (string, error)
func StringsToInts(ss []string) (ints []int, err error)
Filesystem Util

package github.com/gookit/goutil/fsutil

func FileExists(path string) bool
func IsAbsPath(filepath string) bool
func IsDir(path string) bool
func IsFile(path string) bool
func IsZipFile(filepath string) bool
func Mkdir(name string, perm os.FileMode) error
func PathExists(path string) bool
func Unzip(archive, targetDir string) (err error)
JSON Util

package github.com/gookit/goutil/jsonutil

func Decode(json []byte, v interface{}) error
func Encode(v interface{}) ([]byte, error)
func Pretty(v interface{}) (string, error)
func ReadFile(filePath string, v interface{}) error
func WriteFile(filePath string, data interface{}) error
func StripComments(src string) string
Map Util
  • package github.com/gookit/goutil/maputil
func GetByPath(key string, mp map[string]interface{}) (val interface{}, ok bool)
func KeyToLower(src map[string]string) map[string]string
func Keys(mp interface{}) (keys []string)
func MergeStringMap(src, dst map[string]string, ignoreCase bool) map[string]string
func Values(mp interface{}) (values []interface{})
Math Util

package github.com/gookit/goutil/mathutil

func DataSize(size uint64) string
func ElapsedTime(startTime time.Time) string
func Float(s string) (float64, error)
func HowLongAgo(sec int64) string
func Int(in interface{}) (int, error)
func Int64(in interface{}) (int64, error)
func MustFloat(s string) float64
func MustInt(in interface{}) int
func MustInt64(in interface{}) int64
func MustUint(in interface{}) uint64
func Percent(val, total int) float64
func ToFloat(s string) (float64, error)
func ToInt(in interface{}) (iVal int, err error)
func ToInt64(in interface{}) (i64 int64, err error)
func ToUint(in interface{}) (u64 uint64, err error)
func Uint(in interface{}) (uint64, error)
String Util

package github.com/gookit/goutil/strutil

func B64Encode(str string) string
func Bool(s string) (bool, error)
func Camel(s string, sep ...string) string
func CamelCase(s string, sep ...string) string
func FilterEmail(s string) string
func GenMd5(src interface{}) string
func LowerFirst(s string) string
func Lowercase(s string) string
func Md5(src interface{}) string
func MustBool(s string) bool
func MustString(in interface{}) string
func PadLeft(s, pad string, length int) string
func PadRight(s, pad string, length int) string
func Padding(s, pad string, length int, pos uint8) string
func PrettyJSON(v interface{}) (string, error)
func RandomBytes(length int) ([]byte, error)
func RandomString(length int) (string, error)
func RenderTemplate(input string, data interface{}, fns template.FuncMap, isFile ...bool) string
func Repeat(s string, times int) string
func RepeatRune(char rune, times int) (chars []rune)
func Replaces(str string, pairs map[string]string) string
func Similarity(s, t string, rate float32) (float32, bool)
func Snake(s string, sep ...string) string
func SnakeCase(s string, sep ...string) string
func Split(s, sep string) (ss []string)
func String(val interface{}) (string, error)
func Substr(s string, pos, length int) string
func ToArray(s string, sep ...string) []string
func ToBool(s string) (bool, error)
func ToIntSlice(s string, sep ...string) (ints []int, err error)
func ToInts(s string, sep ...string) ([]int, error)
func ToSlice(s string, sep ...string) []string
func ToString(val interface{}) (str string, err error)
func ToTime(s string, layouts ...string) (t time.Time, err error)
func Trim(s string, cutSet ...string) string
func TrimLeft(s string, cutSet ...string) string
func TrimRight(s string, cutSet ...string) string
func URLDecode(s string) string
func URLEncode(s string) string
func UpperFirst(s string) string
func UpperWord(s string) string
func Uppercase(s string) string
System Util

package github.com/gookit/goutil/sysutil

func CurrentShell(onlyName bool) (path string)
func ExecCmd(binName string, args []string, workDir ...string) (string, error)
func HasShellEnv(shell string) bool
func IsConsole(out io.Writer) bool
func IsLinux() bool
func IsMSys() bool
func IsMac() bool
func IsWin() bool
func Kill(pid int, signal syscall.Signal) error
func ProcessExists(pid int) bool
func QuickExec(cmdLine string, workDir ...string) (string, error)
func ShellExec(cmdStr string, shells ...string) (string, error)
Test Util

package github.com/gookit/goutil/testutil

func DiscardStdout() error
func MockEnvValue(key, val string, fn func(nv string))
func MockEnvValues(kvMap map[string]string, fn func())
func MockRequest(h http.Handler, method, path string, data *MD) *httptest.ResponseRecorder
func RestoreStdout() (s string)
func RewriteStdout()

Code Check

gofmt -w -l ./
golint ./... 

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Filling

func Filling(form interface{}, model interface{}) error

Filling filling a model from submitted data form 提交过来的数据结构体 model 定义表模型的数据结构体 相当于是在合并两个结构体(data 必须是 model 的子集)

func FuncName

func FuncName(f interface{}) string

FuncName get func name

func Go

func Go(f func() error) error

Go is a basic promise implementation: it wraps calls a function in a goroutine and returns a channel which will later return the function's return value. from beego/bee

func PkgName

func PkgName() string

PkgName get current package name

Types

type Value

type Value struct {
	// V value
	V interface{}
}

Value data store

func (Value) Bool

func (v Value) Bool() bool

Bool value

func (Value) Float64

func (v Value) Float64() float64

Float64 value

func (Value) Int

func (v Value) Int() int

Int value

func (Value) Int64

func (v Value) Int64() int64

Int64 value

func (Value) IsEmpty

func (v Value) IsEmpty() bool

IsEmpty value

func (*Value) Reset

func (v *Value) Reset()

Reset value

func (Value) String

func (v Value) String() string

String value

func (Value) Strings

func (v Value) Strings() (ss []string)

Strings value

func (Value) Val

func (v Value) Val() interface{}

Val get

Directories

Path Synopsis
Package arrutil provides some util functions for array, slice
Package arrutil provides some util functions for array, slice
Package cliutil provides some util functions for CLI
Package cliutil provides some util functions for CLI
Package dump like fmt.Println but more clear and beautiful print data.
Package dump like fmt.Println but more clear and beautiful print data.
httpctype
Package httpctype list some common http content-type
Package httpctype list some common http content-type

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL