goutil

package module
v0.0.0-...-40cb7ec Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2020 License: MIT Imports: 20 Imported by: 5

README

goutil

Golang 的一些常用函数,很实用。

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Base64Decode

func Base64Decode(s string) ([]byte, error)

Base64Decode .

func Base64Encode

func Base64Encode(data []byte) string

Base64Encode .

func BytesToThumb

func BytesToThumb(img []byte, thumbPath string) error

BytesToThumb creates a thumbnail from img, uses default size and default quality, and write the thumbnail to thumbPath.

func CheckErr

func CheckErr(w http.ResponseWriter, err error, code int) bool

CheckErr 检查 err, 如果有错就以 json 形式返回给前端,并返回 true. 如果没有错误则返回 false.

func CheckErrorFatal

func CheckErrorFatal(err error)

CheckErrorFatal log.Fatal if err != nil

func CheckErrorPanic

func CheckErrorPanic(err error)

CheckErrorPanic panics if err != nil

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) error

DeleteFiles 删除全部 files, 忽略找不到文件的错误,返回其他错误。

func ErrorContains

func ErrorContains(err error, substr string) bool

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

func GetFileContents

func GetFileContents(r *http.Request) ([]byte, error)

GetFileContents gets contents from http.Request.FormFile("file"). It also verifies the file has not been corrupted.

func GetFilesByExt

func GetFilesByExt(dir, ext string) ([]string, error)

GetFilesByExt .

func GetFormValue

func GetFormValue(w http.ResponseWriter, r *http.Request, key string) (value string, ok bool)

GetFormValue checks if the r.FormValue(key) is empty or not, if it is empty, write error message and return false; if it is not empty, return the id and true.

func GetID

func GetID(w http.ResponseWriter, r *http.Request) (string, bool)

GetID .

func HasString

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

HasString reports whether item is in the slice.

func HttpGet

func HttpGet(url string, cookies []*http.Cookie) (*http.Response, error)

HttpGet issues a GET to the specified URL with cookies.

func HttpPostForm

func HttpPostForm(url string, data url.Values, cookies []*http.Cookie) (*http.Response, error)

HttpPostForm issues a POST to the specified URL with cookies, The Content-Type is set to "application/x-www-form-urlencoded".

func JsonMessage

func JsonMessage(w http.ResponseWriter, message string, code int)

JsonMessage 主要用于向前端返回出错消息。

func JsonMsg404

func JsonMsg404(w http.ResponseWriter)

JsonMsg404 ...

func JsonMsgOK

func JsonMsgOK(w http.ResponseWriter)

JsonMsgOK ...

func JsonRequireLogin

func JsonRequireLogin(w http.ResponseWriter)

JsonRequireLogin ...

func JsonResponse

func JsonResponse(w http.ResponseWriter, obj interface{}, code int)

JsonResponse 要用于向前端返回有用数据。 参考 https://stackoverflow.com/questions/59763852/can-you-return-json-in-golang-http-error

func MustMkdir

func MustMkdir(dirName string)

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

func NewID

func NewID() string

NewID 返回一个上升趋势的随机 id, 由时间戳与随机数组成。 时间戳确保其上升趋势(大致有序),随机数确保其随机性(防止被穷举)。 NewID 考虑了 “生成新 id 的速度”、 “并发生成时防止冲突” 与 “id 长度” 这三者的平衡,适用于大多数中、小规模系统(当然,不适用于大型系统)。

func NoCaseContains

func NoCaseContains(s, substr string) bool

NoCaseContains reports whether substr is within s case-insensitive.

func PathIsExist

func PathIsExist(name string) bool

PathIsExist .

func PathIsNotExist

func PathIsNotExist(name string) bool

PathIsNotExist .

func Sha256Hex

func Sha256Hex(data []byte) string

Sha256Hex .

func StringIndex

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

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

func TimeNow

func TimeNow(format string) string

TimeNow output time with format.

func TimestampFilename

func TimestampFilename(ext string) string

TimestampFilename .

func TypeByFilename

func TypeByFilename(filename string) string

TypeByFilename 从文件名中截取后缀名,然后判断文件类型。

func UserHomeDir

func UserHomeDir() string

UserHomeDir .

func WrapErrors

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

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

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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