util

package
v0.0.0-...-a5018ba Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2021 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateFile

func CreateFile(filePath string, src io.Reader) error

CreateFile 把 src 的数据写入 filePath, 权限是 0600, 自动关闭 file.

func CreateReturnFile

func CreateReturnFile(filePath string, src io.Reader) (int64, *os.File, error)

CreateReturnFile 把 src 的数据写入 filePath, 权限是 0600, 会自动创建或覆盖文件,返回 file, 要记得关闭资源。

func DeleteFiles

func DeleteFiles(files []string) (err error)

func ErrorContains

func ErrorContains(err error, substr string) bool

ErrorContains returns NoCaseContains(err.Error(), substr) Returns false if err is nil.

func FileSha256Hex

func FileSha256Hex(name string) (string, error)

FileSha256Hex 返回文件 name 的 hex 字符串。 虽然函数名是 Sha256, 但实际上采用 BLAKE2b 算法。

func FirstLineLimit

func FirstLineLimit(s string, limit int) string

FirstLineLimit 返回第一行,并限定长度,其中 s 必须事先 TrimSpace 并确保不是空字串。 并且, s 也可以事先限制字数. 该函数会尽量确保最后一个字符是有效的 utf8 字符,但当第一行中的全部字符都无效时, 则不保证有效字符。

func GetMIME

func GetMIME(extension string) (mime string)

GetMIME returns the content-type of a file extension. https://github.com/gofiber/fiber/blob/master/utils/http.go (edited).

func GetMarkdownTitle

func GetMarkdownTitle(title string) string

GetMarkdownTitle 截取 markdown 的开头内容获作为标题。 其中 title 应该限制字数 (使用 FirstLineLimit), 没必要传入 markdown 文件的全文。 注意 title 不可包含制表符、回车键等特殊字符。

func HasString

func HasString(slice []string, item string) bool

HasString reports whether item is in the slice.

func MarshalWrite

func MarshalWrite(data interface{}, name string) error

MarshalWrite 把 data 转换为漂亮格式的 JSON 并写入文件 name 中。

func MustMarshal

func MustMarshal(data interface{}) []byte

MustMarshal .

func MustMarshalWrite

func MustMarshalWrite(data interface{}, name string)

MustMarshalWrite 把 data 转换为漂亮格式的 JSON 并写入文件 name 中。

func MustMkdir

func MustMkdir(dirName string)

MustMkdir 确保有一个名为 dirName 的文件夹, 如果没有则自动创建,如果已存在则不进行任何操作。

func Panic

func Panic(err error)

Panic panics if err != nil

func PathIsExist

func PathIsExist(name string) (bool, error)

PathIsExist .

func PathIsNotExist

func PathIsNotExist(name string) (bool, error)

PathIsNotExist .

func Sha256Hex

func Sha256Hex(data []byte) string

Sha256Hex 返回 sha256 的 hex 字符串。 虽然函数名是 Sha256, 但实际上采用 BLAKE2b 算法。

func StrSliceDiff

func StrSliceDiff(newArr, oldArr []string) (toAdd, toDelete []string)

StrSliceDiff 对比新旧数组的差异,并返回需要新增的项目与需要删除的项目。

func StrSliceRemove

func StrSliceRemove(slice []string, i int) []string

StrSliceRemove 删除 slice 中的第 i 个元素。

func StringIndex

func StringIndex(slice []string, item string) int

StringIndex returns the index of item in the slice. returns -1 if not found.

func UserHomeDir

func UserHomeDir() string

UserHomeDir 就是 os.UserHomeDir

func WrapErrors

func WrapErrors(allErrors ...error) (wrapped error)

WrapErrors 把多个错误合并为一个错误.

Types

This section is empty.

Jump to

Keyboard shortcuts

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