1 Star 0 Fork 0

lorock / goutil

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

Go Util

GitHub go.mod Go version GoDoc Go Report Card Build Status Coverage Status

Go一些常用的工具函数收集、实现和整理

EN README

  • arrutil array/slice util
  • dump 简单的变量打印工具,打印 slice, map 会自动换行显示每个元素,同时会显示打印调用位置
  • cliutil CLI util
  • envutil ENV util
  • 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
  • goaddr getaddr

GoDoc

Util Packages

Array/Slice

package gitee.com/lorock/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 gitee.com/lorock/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 gitee.com/lorock/goutil/dump

简单的变量打印工具,打印 slice, map 会自动换行显示每个元素,同时会显示打印调用位置

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

使用请查看 dump/README.md

ENV Util

package gitee.com/lorock/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 gitee.com/lorock/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 gitee.com/lorock/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 gitee.com/lorock/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 gitee.com/lorock/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 gitee.com/lorock/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 gitee.com/lorock/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 gitee.com/lorock/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 gitee.com/lorock/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()

License

MIT

The MIT License (MIT) Copyright (c) 2016 inhere Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

Helper Utils For The Go: string, array/slice, map, format, cli, env, filesystem, test and more. Go 的一些工具函数,格式化,特殊处理,常用信息获取等等 展开 收起
MIT
取消

发行版 (1)

全部

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/lorock/goutil.git
git@gitee.com:lorock/goutil.git
lorock
goutil
goutil
master

搜索帮助