Documentation
¶
Overview ¶
Package misc 提供通用的实用工具函数与类型:
- 摘要/密码:MD5、Sha1、Sha256、PasswordHash/Verify
- 函数组合:Call/CallWith、Wrap/WrapWith、MustCall/MustCallWith
- MIME 解析:ExtensionByType、TypeByExtension、CharsetByType
- 模板插值:Strtr/Tmpl、Interpolate、TagFunc
- 堆栈信息:Stack(包含源码行)
- 零拷贝转换:UnsafeBytesToString、UnsafeStringToBytes 等(需谨慎使用)
- 零值判断:IsZero(支持指针递归判断)
Index ¶
- func BytesToString(b []byte) string
- func Call(fns ...func() error) error
- func CallWith[T any](val T, fns ...func(T) error) error
- func CharsetByType(typ string) string
- func ExtensionByType(mimeType string) string
- func Interpolate(template, startTag, endTag string, w io.Writer, f TagFunc) (int64, error)
- func IsZero[T any](v T) bool
- func MD5(str string) string
- func MustCall(fns ...func() error)
- func MustCallWith[T any](val T, fns ...func(T) error)
- func PasswordHash(password string) (string, error)
- func PasswordVerify(password, hash string) bool
- func Sha1(str string) string
- func Sha256(str string) string
- func SliceByteToString(b []byte) string
- func Stack(skip int) string
- func StringToBytes(s string) []byte
- func StringToSliceByte(s string) []byte
- func Strtr(template string, data map[string]any) (string, error)
- func Tmpl(template, startTag, endTag string, data map[string]any) (string, error)
- func TypeByExtension(ext string) string
- func UnsafeBytesToString(b []byte) string
- func UnsafeStringToBytes(s string) []byte
- func Wrap(fns ...func() error) func() error
- func WrapWith[T any](fns ...func(T) error) func(val T) error
- type TagFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BytesToString ¶
BytesToString 使用 Go 1.20+ 的 unsafe 辅助 API 将字节切片零拷贝转换为字符串。 注意:结果与原切片共享底层数据,需保证原切片后续不被修改。
func CharsetByType ¶
CharsetByType 针对 text/* 类型返回 "charset=utf-8",否则返回空字符串。
func ExtensionByType ¶
ExtensionByType 根据 MIME 类型返回一个匹配的扩展名(包含点,如 .png)。 优先使用内置表,其次回退到标准库 mime.ExtensionsByType。 若无法识别,返回空字符串。
func Interpolate ¶
Interpolate calls f on each template tag (placeholder) occurrence.
Returns the number of bytes written to w.
func IsZero ¶
IsZero 泛型零值判断 https://stackoverflow.com/questions/74000242/in-golang-how-to-compare-interface-as-generics-type-to-nil IsZero 判断任意泛型值是否为零值。 若为指针类型,会递归地解引用直至基础值再判断。
func MustCallWith ¶
MustCallWith 依次调用函数并传入相同参数;若出现错误,打印函数名并以 fatal 退出程序。
func PasswordHash ¶
PasswordHash 使用 bcrypt 生成口令哈希。 返回的哈希可用于后续的 PasswordVerify 校验。
func PasswordVerify ¶
PasswordVerify 使用 bcrypt 对明文口令与哈希进行校验。 返回 true 表示匹配,false 表示不匹配。
func SliceByteToString ¶
SliceByteToString convert a byte slice to string Deprecated: use UnsafeBytesToString func SliceByteToString 将字节切片转换为字符串(零拷贝)。 注意:属于不安全转换,底层数据共享;若切片内容随后被修改,将影响字符串视图。
func Stack ¶
Stack will skip back the provided number of frames and return a stack trace with source code. Although we could just use debug.Stack(), this routine will return the source code and skip back the provided number of frames - i.e. allowing us to ignore preceding function calls. A skip of 0 returns the stack trace for the calling function, not including this call. If the problem is a lack of memory of course all this is not going to work... Stack 返回带源码行的堆栈信息。参数 skip 用于跳过前置帧(0 表示调用者)。 与 debug.Stack 不同之处在于:本函数尝试同时输出对应源码行,方便定位。
func StringToBytes ¶
StringToBytes 使用 Go 1.20+ 的 unsafe 辅助 API 将字符串零拷贝转换为字节切片。 注意:得到的切片只读,切勿修改其内容。
func StringToSliceByte ¶
StringToSliceByte convert a string to byte slice. Deprecated: use UnsafeStringToBytes func StringToSliceByte 将字符串转换为字节切片(零拷贝)。 注意:属于不安全转换,返回切片不可修改(否则会触发未定义行为)。
func TypeByExtension ¶
TypeByExtension 根据扩展名(可不含点)返回对应的 MIME 类型。 优先匹配内置表,不存在时回退到标准库 mime.TypeByExtension。
func UnsafeBytesToString ¶
UnsafeBytesToString uses Go's unsafe package to convert a byte slice to a string. Deprecated: use BytesToString instead. UnsafeBytesToString 使用 Go 1.20+ 的 unsafe 辅助 API 进行零拷贝转换。 注意:结果与原切片共享底层数据,需保证原切片后续不被修改。
func UnsafeStringToBytes ¶
UnsafeStringToBytes uses Go's unsafe package to convert a string to a byte slice. Deprecated: use StringToBytes instead. UnsafeStringToBytes 使用 Go 1.20+ 的 unsafe 辅助 API 将字符串零拷贝转换为字节切片。 注意:得到的切片只读,切勿修改其内容。
Types ¶
type TagFunc ¶
TagFunc can be used as a substitution value in the map passed to Interpolate. Interpolate functions pass tag (placeholder) name in 'tag' argument.
TagFunc must be safe to call from concurrently running goroutines.
TagFunc must write contents to w and return the number of bytes written.