gulu

package module
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2022 License: MulanPSL-2.0 Imports: 27 Imported by: 87

README

Gulu

Go 语言常用工具库,这个轱辘还算圆!




💡 简介

Gulu 是一款 Go 语言常用工具库。

欢迎到 Gulu 官方讨论区了解更多。同时也欢迎关注 B3log 开源社区微信公众号 B3log开源

b3logos.jpg

✨ 功能

文件操作 gulu.File

  • 获取文件大小
  • 判断路径是否存在
  • 判断文件是否是图片
  • 按内容判断文件是否是可执行二进制
  • 判断文件是否是目录
  • 复制文件
  • 复制目录

Go 语言 gulu.Go

  • 获取 Go API 源码目录路径
  • 判断指定路径是否在 Go API 源码目录下
  • 获取格式化工具名 ["gofmt", "goimports"]
  • 获取 $GOBIN 下指定可执行程序名的绝对路径

日志记录 gulu.Log

  • 提供可指定日志级别的日志记录器

网络相关 gulu.Net

  • 获取本机第一张网卡的 IP 地址
  • 获取本机第一张网卡的 MAC 地址

操作系统 gulu.OS

  • 判断是否是 Windows
  • 判断是否是 Linux
  • 判断是否是 Darwin
  • 获取当前进程的工作目录
  • 获取用户 Home 目录路径

panic 处理 gulu.Panic

  • 包装 recover() 提供更好的报错日志格式

随机数 gulu.Rand

  • 随机字符串
  • 随机整数

返回值 gulu.Ret

  • 提供普适返回值结构

Rune gulu.Rune

  • 判断 rune 是否为数字或字母
  • 判断 rune 是否为字母

字符串 gulu.Str

  • 字符串是否包含在字符串数组中
  • 字符串忽略大小写批量查找替换
  • 字符串忽略大小写批量查找环绕
  • 求最长公共子串
  • 考虑 Rune 的字符串指定长度子串

Zip 压缩解压 gulu.Zip

  • Zip 压缩和解压

🗃 案例

  • Pipe:一款小而美的博客平台,专为程序员设计
  • Wide:一款基于 Web 的 Go 语言 IDE,随时随地玩 golang
  • BND:一款图形界面的百度网盘不限速下载器,支持 Windows、Linux 和 Mac
  • 协慌网:专注编程问答汉化
  • 链滴笔记:一款桌面端笔记应用,支持 Windows、Mac 和 Linux

💝 贡献

Gulu 肯定有一些不足之处:

  • 代码不够优美
  • 文档不够清晰
  • 功能不够完善
  • 可能存在缺陷
  • ……

希望大家能和我们一起来完善该项目,无论是提交需求建议还是代码改进,我们都非常欢迎!

🏘️ 社区

📄 授权

Gulu 使用 木兰宽松许可证, 第2版 开源协议。

🙏 鸣谢

Documentation

Overview

Package gulu implements some common utilities.

Index

Constants

View Source
const (
	Off = iota
	Trace
	Debug
	Info
	Warn
	Error
	Fatal
)

Logging level.

View Source
const (
	// ZWSP represents zero-width space.
	ZWSP = '\u200B'

	// ZWNBSP represents zero-width no-break space.
	ZWNBSP = '\uFEFF'

	// ZWJ represents zero-width joiner.
	ZWJ = '\u200D'

	// ZWNJ represents zero-width non-joiner.
	ZWNJ = '\u200C'
)

Variables

View Source
var (
	// File utilities
	File GuluFile
	// Go utilities
	Go GuluGo
	// Net utilities
	Net GuluNet
	// OS utilities
	OS GuluOS
	// Panic utilities
	Panic GuluPanic
	// Rand utilities
	Rand GuluRand
	// Ret utilities
	Ret GuluRet
	// Rune utilities
	Rune GuluRune
	// Str utilities
	Str GuluStr
	// Zip utilities
	Zip GuluZip
	// Tar utilities
	Tar GuluTar
	// JSON utilities
	JSON GuluJSON
)
View Source
var Log = guluLog{}

Log utilities.

Functions

This section is empty.

Types

type GuluFile

type GuluFile byte

GuluFile is the receiver of file utilities

func (*GuluFile) Copy added in v1.1.8

func (gl *GuluFile) Copy(source, dest string) (err error)

Copy copies the source to the dest. Keep the dest access/mod time as the same as the source.

func (*GuluFile) CopyDir

func (gl *GuluFile) CopyDir(source, dest string) (err error)

CopyDir copies the source directory to the dest directory. Keep the dest access/mod time as the same as the source.

func (*GuluFile) CopyDirNewtimes added in v1.1.8

func (gl *GuluFile) CopyDirNewtimes(source, dest string) (err error)

CopyDirNewtimes copies the source directory to the dest directory. Do not keep the dest access/mod time as the same as the source.

func (*GuluFile) CopyFile

func (gl *GuluFile) CopyFile(source, dest string) (err error)

CopyFile copies the source file to the dest file. Keep the dest access/mod time as the same as the source.

func (*GuluFile) CopyFileNewtimes added in v1.1.8

func (gl *GuluFile) CopyFileNewtimes(source, dest string) (err error)

CopyFileNewtimes copies the source file to the dest file. Do not keep the dest access/mod time as the same as the source.

func (*GuluFile) CopyNewtimes added in v1.1.8

func (gl *GuluFile) CopyNewtimes(source, dest string) (err error)

CopyNewtimes copies the source to the dest. Do not keep the dest access/mod time as the same as the source.

func (*GuluFile) GetFileSize

func (*GuluFile) GetFileSize(path string) int64

GetFileSize get the length in bytes of file of the specified path.

func (*GuluFile) IsBinary

func (*GuluFile) IsBinary(content string) bool

IsBinary determines whether the specified content is a binary file content.

func (*GuluFile) IsDir

func (*GuluFile) IsDir(path string) bool

IsDir determines whether the specified path is a directory.

func (*GuluFile) IsExist

func (*GuluFile) IsExist(path string) bool

IsExist determines whether the file spcified by the given path is exists.

func (*GuluFile) IsHidden added in v1.1.8

func (*GuluFile) IsHidden(path string) bool

IsHidden checks whether the file specified by the given path is hidden.

func (*GuluFile) IsImg

func (*GuluFile) IsImg(extension string) bool

IsImg determines whether the specified extension is a image.

func (GuluFile) IsValidFilename added in v1.2.2

func (GuluFile) IsValidFilename(name string) bool

func (GuluFile) WriteFileSafer added in v1.1.8

func (GuluFile) WriteFileSafer(writePath string, data []byte, perm os.FileMode) error

WriteFileSafer writes the data to a temp file and atomically move if everything else succeeds.

func (GuluFile) WriteFileSaferByHandle added in v1.1.8

func (GuluFile) WriteFileSaferByHandle(handle *os.File, data []byte) error

WriteFileSaferByHandle writes the data to a temp file and writes the original file if everything else succeeds. Note: This function does not close the file handle after writing data.

func (GuluFile) WriteFileSaferByReader added in v1.1.8

func (GuluFile) WriteFileSaferByReader(writePath string, reader io.Reader, perm os.FileMode) error

WriteFileSaferByReader writes the data to a temp file and atomically move if everything else succeeds.

type GuluGo

type GuluGo byte

GuluGo is the receiver of Go utilities

func (*GuluGo) GetAPIPath

func (*GuluGo) GetAPIPath() string

GetAPIPath gets the Go source code path $GOROOT/src.

func (*GuluGo) GetExecutableInGOBIN

func (*GuluGo) GetExecutableInGOBIN(executable string) string

GetExecutableInGOBIN gets executable file under GOBIN path.

The specified executable should not with extension, this function will append .exe if on Windows.

func (*GuluGo) GetGoFormats

func (*GuluGo) GetGoFormats() []string

GetGoFormats gets Go format tools. It may return ["gofmt", "goimports"].

func (*GuluGo) IsAPI

func (*GuluGo) IsAPI(path string) bool

IsAPI determines whether the specified path belongs to Go API.

type GuluJSON added in v1.1.8

type GuluJSON byte

GuluJSON is the receiver of JSON utilities

func (*GuluJSON) MarshalIndentJSON added in v1.1.8

func (*GuluJSON) MarshalIndentJSON(v interface{}, prefix, indent string) ([]byte, error)

func (*GuluJSON) MarshalJSON added in v1.1.8

func (*GuluJSON) MarshalJSON(v interface{}) ([]byte, error)

func (*GuluJSON) UnmarshalJSON added in v1.1.8

func (*GuluJSON) UnmarshalJSON(data []byte, v interface{}) error

type GuluNet

type GuluNet byte

GuluNet is the receiver of network utilities

func (*GuluNet) LocalIP

func (*GuluNet) LocalIP() (string, error)

LocalIP gets the first NIC's IP address.

func (*GuluNet) LocalMac

func (*GuluNet) LocalMac() (string, error)

LocalMac gets the first NIC's MAC address.

type GuluOS

type GuluOS byte

GuluOS is the receiver of OS utilities

func (*GuluOS) Home

func (*GuluOS) Home() (string, error)

Home returns the home directory for the executing user.

This uses an OS-specific method for discovering the home directory. An error is returned if a home directory cannot be detected.

func (*GuluOS) IsDarwin added in v1.1.8

func (*GuluOS) IsDarwin() bool

IsDarwin determines whether current OS is Darwin.

func (*GuluOS) IsLinux added in v1.1.8

func (*GuluOS) IsLinux() bool

IsLinux determines whether current OS is Linux.

func (*GuluOS) IsWindows

func (*GuluOS) IsWindows() bool

IsWindows determines whether current OS is Windows.

func (*GuluOS) Pwd

func (*GuluOS) Pwd() string

Pwd gets the path of current working directory.

type GuluPanic

type GuluPanic byte

GuluPanic is the receiver of panic utilities

func (*GuluPanic) Recover

func (*GuluPanic) Recover(err *error)

Recover recovers a panic.

type GuluRand

type GuluRand byte

GuluRand is the receiver of random utilities

func (*GuluRand) Int

func (*GuluRand) Int(min int, max int) int

Int returns a random integer in range [min, max].

func (*GuluRand) Ints

func (*GuluRand) Ints(from, to, size int) []int

Ints returns a random integer array with the specified from, to and size.

func (*GuluRand) String

func (*GuluRand) String(length int) string

String returns a random string ['a', 'z'] and ['0', '9'] in the specified length.

type GuluRet

type GuluRet byte

GuluRet is the receiver of result utilities

func (*GuluRet) NewResult

func (*GuluRet) NewResult() *Result

NewResult creates a result with Code=0, Msg="", Data=nil.

func (*GuluRet) RetGzJSON

func (*GuluRet) RetGzJSON(w http.ResponseWriter, r *http.Request, res map[string]interface{})

RetGzJSON writes HTTP response with "Content-Type, application/json" and "Content-Encoding, gzip".

func (*GuluRet) RetGzResult

func (*GuluRet) RetGzResult(w http.ResponseWriter, r *http.Request, res *Result)

RetGzResult writes HTTP response with "Content-Type, application/json" and "Content-Encoding, gzip".

func (*GuluRet) RetJSON

func (*GuluRet) RetJSON(w http.ResponseWriter, r *http.Request, res map[string]interface{})

RetJSON writes HTTP response with "Content-Type, application/json".

func (*GuluRet) RetResult

func (*GuluRet) RetResult(w http.ResponseWriter, r *http.Request, res *Result)

RetResult writes HTTP response with "Content-Type, application/json".

type GuluRune

type GuluRune byte

GuluRune is the receiver of rune utilities

func (*GuluRune) ContainChinese added in v1.1.8

func (*GuluRune) ContainChinese(str string) bool

ContainChinese checks the specified string whether contains chinese.

func (*GuluRune) IsLetter

func (*GuluRune) IsLetter(r rune) bool

IsLetter checks the specified rune is letter.

func (*GuluRune) IsNumOrLetter

func (*GuluRune) IsNumOrLetter(r rune) bool

IsNumOrLetter checks the specified rune is number or letter.

type GuluStr

type GuluStr byte

GuluStr is the receiver of string utilities

func (*GuluStr) Contains

func (*GuluStr) Contains(str string, strs []string) bool

Contains determines whether the str is in the strs.

func (*GuluStr) EncloseIgnoreCase added in v1.1.8

func (*GuluStr) EncloseIgnoreCase(text, open, close string, searchStrs ...string) string

Enclose encloses search strings with open and close, case-insensitively.

func (*GuluStr) FromBytes

func (*GuluStr) FromBytes(bytes []byte) string

FromBytes converts the specified byte array to a string.

func (*GuluStr) HasZeroWidthCharacters added in v1.2.2

func (*GuluStr) HasZeroWidthCharacters(s string) bool

HasZeroWidthCharacters reports whether string s contains zero-width characters.

func (*GuluStr) IsASCII added in v1.2.2

func (*GuluStr) IsASCII(s string) bool

func (*GuluStr) LCS

func (*GuluStr) LCS(s1 string, s2 string) string

LCS gets the longest common substring of s1 and s2.

Refers to http://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Longest_common_substring.

func (*GuluStr) RemoveCtl added in v1.2.2

func (*GuluStr) RemoveCtl(str string) string

RemoveCtl removes all control characters from string str.

func (*GuluStr) RemoveInvisible added in v1.2.2

func (gs *GuluStr) RemoveInvisible(str string) string

RemoveInvisible removes invisible characters from string str.

func (*GuluStr) RemoveZeroWidthCharacters added in v1.2.2

func (*GuluStr) RemoveZeroWidthCharacters(s string) string

RemoveZeroWidthCharacters removes all zero-width characters from string s.

func (*GuluStr) RemoveZeroWidthJoiner added in v1.2.2

func (*GuluStr) RemoveZeroWidthJoiner(s string) string

RemoveZeroWidthJoiner removes zero-width joiner characters from string s.

func (*GuluStr) RemoveZeroWidthNoBreakSpace added in v1.2.2

func (*GuluStr) RemoveZeroWidthNoBreakSpace(s string) string

RemoveZeroWidthNoBreakSpace removes zero-width no-break space characters from string s.

func (*GuluStr) RemoveZeroWidthNonJoiner added in v1.2.2

func (*GuluStr) RemoveZeroWidthNonJoiner(s string) string

RemoveZeroWidthNonJoiner removes zero-width non-joiner characters from string s.

func (*GuluStr) RemoveZeroWidthSpace added in v1.2.2

func (*GuluStr) RemoveZeroWidthSpace(s string) string

RemoveZeroWidthSpace removes zero-width space characters from string s.

func (*GuluStr) ReplaceIgnoreCase added in v1.1.8

func (*GuluStr) ReplaceIgnoreCase(text, searchStr, repl string) string

ReplaceIgnoreCase replace searchStr with repl in the text, case-insensitively.

func (*GuluStr) ReplacesIgnoreCase added in v1.1.8

func (*GuluStr) ReplacesIgnoreCase(text string, searchStrRepl ...string) string

ReplacesIgnoreCase replace searchStr-repl pairs in the text, case-insensitively.

func (*GuluStr) SubStr added in v1.1.8

func (*GuluStr) SubStr(str string, length int) (ret string)

SubStr decode str into runes and get substring with the specified length.

func (*GuluStr) SubstringsBetween added in v1.2.2

func (*GuluStr) SubstringsBetween(str, start, end string) (ret []string)

SubstringsBetween returns a slice of sub strings between the start and end.

func (*GuluStr) ToBytes

func (*GuluStr) ToBytes(str string) []byte

ToBytes converts the specified str to a byte array.

type GuluTar added in v1.1.8

type GuluTar byte

GuluTar is the receiver of tar utilities

func (*GuluTar) Tar added in v1.1.8

func (*GuluTar) Tar(source, target string) error

func (*GuluTar) Untar added in v1.1.8

func (*GuluTar) Untar(tarball, target string) error

type GuluZip

type GuluZip byte

GuluZip is the receiver of zip utilities

func (*GuluZip) Create

func (*GuluZip) Create(filename string) (*ZipFile, error)

Create creates a zip file with the specified filename.

func (*GuluZip) Unzip

func (*GuluZip) Unzip(zipFilePath, destination string) error

Unzip extracts a zip file specified by the zipFilePath to the destination.

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

Logger represents a simple logger with level. The underlying logger is the standard Go logging "log".

func (*Logger) Debug

func (l *Logger) Debug(v ...interface{})

Debug prints debug level message.

func (*Logger) Debugf

func (l *Logger) Debugf(format string, v ...interface{})

Debugf prints debug level message with format.

func (*Logger) Error

func (l *Logger) Error(v ...interface{})

Error prints error level message.

func (*Logger) Errorf

func (l *Logger) Errorf(format string, v ...interface{})

Errorf prints error level message with format.

func (*Logger) Fatal

func (l *Logger) Fatal(v ...interface{})

Fatal prints fatal level message and exit process with code 1.

func (*Logger) Fatalf

func (l *Logger) Fatalf(format string, v ...interface{})

Fatalf prints fatal level message with format and exit process with code 1.

func (*Logger) Info

func (l *Logger) Info(v ...interface{})

Info prints info level message.

func (*Logger) Infof

func (l *Logger) Infof(format string, v ...interface{})

Infof prints info level message with format.

func (*Logger) IsDebugEnabled

func (l *Logger) IsDebugEnabled() bool

IsDebugEnabled determines whether the debug level is enabled.

func (*Logger) IsTraceEnabled

func (l *Logger) IsTraceEnabled() bool

IsTraceEnabled determines whether the trace level is enabled.

func (*Logger) IsWarnEnabled

func (l *Logger) IsWarnEnabled() bool

IsWarnEnabled determines whether the debug level is enabled.

func (*Logger) SetLevel

func (l *Logger) SetLevel(level string)

SetLevel sets the logging level of a logger.

func (*Logger) Trace

func (l *Logger) Trace(v ...interface{})

Trace prints trace level message.

func (*Logger) Tracef

func (l *Logger) Tracef(format string, v ...interface{})

Tracef prints trace level message with format.

func (*Logger) Warn

func (l *Logger) Warn(v ...interface{})

Warn prints warning level message.

func (*Logger) Warnf

func (l *Logger) Warnf(format string, v ...interface{})

Warnf prints warning level message with format.

type Result

type Result struct {
	Code int         `json:"code"` // return code
	Msg  string      `json:"msg"`  // message
	Data interface{} `json:"data"` // data object
}

Result represents a common-used result struct.

type ZipFile

type ZipFile struct {
	// contains filtered or unexported fields
}

ZipFile represents a zip file.

func (*ZipFile) AddDirectory

func (z *ZipFile) AddDirectory(path, dirName string) error

AddDirectory adds a directory.

func (*ZipFile) AddDirectoryN

func (z *ZipFile) AddDirectoryN(path string, names ...string) error

AddDirectoryN adds directories.

func (*ZipFile) AddEntry

func (z *ZipFile) AddEntry(path, name string) error

AddEntry adds a entry.

func (*ZipFile) AddEntryN

func (z *ZipFile) AddEntryN(path string, names ...string) error

AddEntryN adds entries.

func (*ZipFile) Close

func (z *ZipFile) Close() error

Close closes the zip file writer.

Jump to

Keyboard shortcuts

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