README
¶
💡 简介
Gulu 是一款 Go 语言常用工具库。
欢迎到 Gulu 官方讨论区了解更多。同时也欢迎关注 B3log 开源社区微信公众号 B3log开源
:
✨ 功能
文件操作 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
- Variables
- type GuluFile
- func (gl *GuluFile) Copy(source, dest string) (err error)
- func (gl *GuluFile) CopyDir(source, dest string) (err error)
- func (gl *GuluFile) CopyDirNewtimes(source, dest string) (err error)
- func (gl *GuluFile) CopyFile(source, dest string) (err error)
- func (gl *GuluFile) CopyFileNewtimes(source, dest string) (err error)
- func (gl *GuluFile) CopyNewtimes(source, dest string) (err error)
- func (*GuluFile) GetFileSize(path string) int64
- func (*GuluFile) IsBinary(content string) bool
- func (*GuluFile) IsDir(path string) bool
- func (*GuluFile) IsExist(path string) bool
- func (*GuluFile) IsHidden(path string) bool
- func (*GuluFile) IsImg(extension string) bool
- func (GuluFile) IsValidFilename(name string) bool
- func (GuluFile) WriteFileSafer(writePath string, data []byte, perm os.FileMode) error
- func (GuluFile) WriteFileSaferByHandle(handle *os.File, data []byte) error
- func (GuluFile) WriteFileSaferByReader(writePath string, reader io.Reader, perm os.FileMode) error
- type GuluGo
- type GuluJSON
- type GuluNet
- type GuluOS
- type GuluPanic
- type GuluRand
- type GuluRet
- func (*GuluRet) NewResult() *Result
- func (*GuluRet) RetGzJSON(w http.ResponseWriter, r *http.Request, res map[string]interface{})
- func (*GuluRet) RetGzResult(w http.ResponseWriter, r *http.Request, res *Result)
- func (*GuluRet) RetJSON(w http.ResponseWriter, r *http.Request, res map[string]interface{})
- func (*GuluRet) RetResult(w http.ResponseWriter, r *http.Request, res *Result)
- type GuluRune
- type GuluStr
- func (*GuluStr) Contains(str string, strs []string) bool
- func (*GuluStr) EncloseIgnoreCase(text, open, close string, searchStrs ...string) string
- func (*GuluStr) FromBytes(bytes []byte) string
- func (*GuluStr) HasZeroWidthCharacters(s string) bool
- func (*GuluStr) IsASCII(s string) bool
- func (*GuluStr) LCS(s1 string, s2 string) string
- func (*GuluStr) RemoveCtl(str string) string
- func (gs *GuluStr) RemoveInvisible(str string) string
- func (*GuluStr) RemoveZeroWidthCharacters(s string) string
- func (*GuluStr) RemoveZeroWidthJoiner(s string) string
- func (*GuluStr) RemoveZeroWidthNoBreakSpace(s string) string
- func (*GuluStr) RemoveZeroWidthNonJoiner(s string) string
- func (*GuluStr) RemoveZeroWidthSpace(s string) string
- func (*GuluStr) ReplaceIgnoreCase(text, searchStr, repl string) string
- func (*GuluStr) ReplacesIgnoreCase(text string, searchStrRepl ...string) string
- func (*GuluStr) SubStr(str string, length int) (ret string)
- func (*GuluStr) SubstringsBetween(str, start, end string) (ret []string)
- func (*GuluStr) ToBytes(str string) []byte
- type GuluTar
- type GuluZip
- type Logger
- func (l *Logger) Debug(v ...interface{})
- func (l *Logger) Debugf(format string, v ...interface{})
- func (l *Logger) Error(v ...interface{})
- func (l *Logger) Errorf(format string, v ...interface{})
- func (l *Logger) Fatal(v ...interface{})
- func (l *Logger) Fatalf(format string, v ...interface{})
- func (l *Logger) Info(v ...interface{})
- func (l *Logger) Infof(format string, v ...interface{})
- func (l *Logger) IsDebugEnabled() bool
- func (l *Logger) IsTraceEnabled() bool
- func (l *Logger) IsWarnEnabled() bool
- func (l *Logger) SetLevel(level string)
- func (l *Logger) Trace(v ...interface{})
- func (l *Logger) Tracef(format string, v ...interface{})
- func (l *Logger) Warn(v ...interface{})
- func (l *Logger) Warnf(format string, v ...interface{})
- type Result
- type ZipFile
Constants ¶
const ( Off = iota Trace Debug Info Warn Error Fatal )
Logging level.
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 ¶
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 )
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
Copy copies the source to the dest. Keep the dest access/mod time as the same as the source.
func (*GuluFile) CopyDir ¶
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
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 ¶
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
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
CopyNewtimes copies the source to the dest. Do not keep the dest access/mod time as the same as the source.
func (*GuluFile) GetFileSize ¶
GetFileSize get the length in bytes of file of the specified path.
func (*GuluFile) IsBinary ¶
IsBinary determines whether the specified content is a binary file content.
func (*GuluFile) IsExist ¶
IsExist determines whether the file spcified by the given path is exists.
func (*GuluFile) IsHidden ¶ added in v1.1.8
IsHidden checks whether the file specified by the given path is hidden.
func (GuluFile) IsValidFilename ¶ added in v1.2.2
func (GuluFile) WriteFileSafer ¶ added in v1.1.8
WriteFileSafer writes the data to a temp file and atomically move if everything else succeeds.
func (GuluFile) WriteFileSaferByHandle ¶ added in v1.1.8
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.
type GuluGo ¶
type GuluGo byte
GuluGo is the receiver of Go utilities
func (*GuluGo) GetAPIPath ¶
GetAPIPath gets the Go source code path $GOROOT/src.
func (*GuluGo) GetExecutableInGOBIN ¶
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 ¶
GetGoFormats gets Go format tools. It may return ["gofmt", "goimports"].
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) MarshalJSON ¶ added in v1.1.8
func (*GuluJSON) UnmarshalJSON ¶ added in v1.1.8
type GuluNet ¶
type GuluNet byte
GuluNet is the receiver of network utilities
type GuluOS ¶
type GuluOS byte
GuluOS is the receiver of OS utilities
func (*GuluOS) Home ¶
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.
type GuluRand ¶
type GuluRand byte
GuluRand is the receiver of random utilities
type GuluRet ¶
type GuluRet byte
GuluRet is the receiver of result utilities
func (*GuluRet) RetGzJSON ¶
RetGzJSON writes HTTP response with "Content-Type, application/json" and "Content-Encoding, gzip".
func (*GuluRet) RetGzResult ¶
RetGzResult writes HTTP response with "Content-Type, application/json" and "Content-Encoding, gzip".
type GuluRune ¶
type GuluRune byte
GuluRune is the receiver of rune utilities
func (*GuluRune) ContainChinese ¶ added in v1.1.8
ContainChinese checks the specified string whether contains chinese.
func (*GuluRune) IsNumOrLetter ¶
IsNumOrLetter checks the specified rune is number or letter.
type GuluStr ¶
type GuluStr byte
GuluStr is the receiver of string utilities
func (*GuluStr) EncloseIgnoreCase ¶ added in v1.1.8
Enclose encloses search strings with open and close, case-insensitively.
func (*GuluStr) HasZeroWidthCharacters ¶ added in v1.2.2
HasZeroWidthCharacters reports whether string s contains zero-width characters.
func (*GuluStr) LCS ¶
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
RemoveCtl removes all control characters from string str.
func (*GuluStr) RemoveInvisible ¶ added in v1.2.2
RemoveInvisible removes invisible characters from string str.
func (*GuluStr) RemoveZeroWidthCharacters ¶ added in v1.2.2
RemoveZeroWidthCharacters removes all zero-width characters from string s.
func (*GuluStr) RemoveZeroWidthJoiner ¶ added in v1.2.2
RemoveZeroWidthJoiner removes zero-width joiner characters from string s.
func (*GuluStr) RemoveZeroWidthNoBreakSpace ¶ added in v1.2.2
RemoveZeroWidthNoBreakSpace removes zero-width no-break space characters from string s.
func (*GuluStr) RemoveZeroWidthNonJoiner ¶ added in v1.2.2
RemoveZeroWidthNonJoiner removes zero-width non-joiner characters from string s.
func (*GuluStr) RemoveZeroWidthSpace ¶ added in v1.2.2
RemoveZeroWidthSpace removes zero-width space characters from string s.
func (*GuluStr) ReplaceIgnoreCase ¶ added in v1.1.8
ReplaceIgnoreCase replace searchStr with repl in the text, case-insensitively.
func (*GuluStr) ReplacesIgnoreCase ¶ added in v1.1.8
ReplacesIgnoreCase replace searchStr-repl pairs in the text, case-insensitively.
func (*GuluStr) SubStr ¶ added in v1.1.8
SubStr decode str into runes and get substring with the specified length.
func (*GuluStr) SubstringsBetween ¶ added in v1.2.2
SubstringsBetween returns a slice of sub strings between the start and end.
type GuluZip ¶
type GuluZip byte
GuluZip is the receiver of zip utilities
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) Fatal ¶
func (l *Logger) Fatal(v ...interface{})
Fatal prints fatal level message and exit process with code 1.
func (*Logger) IsDebugEnabled ¶
IsDebugEnabled determines whether the debug level is enabled.
func (*Logger) IsTraceEnabled ¶
IsTraceEnabled determines whether the trace level is enabled.
func (*Logger) IsWarnEnabled ¶
IsWarnEnabled determines whether the debug level is enabled.
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 ¶
AddDirectory adds a directory.
func (*ZipFile) AddDirectoryN ¶
AddDirectoryN adds directories.