util

package
v0.0.0-...-1bd18f3 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2026 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Overview

Package util provides common utility functions Package util 提供通用工具函数

Package util provides common utility functions Package util 提供通用工具函数

Package util provides common utility functions Package util 提供通用工具函数

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyDefaultFolder

func ApplyDefaultFolder(path, defaultFolder string) string

ApplyDefaultFolder applies default folder prefix ApplyDefaultFolder 应用默认文件夹前缀 When path does not contain "/" and defaultFolder is not empty, add defaultFolder as prefix to path 当 path 不包含 "/" 且 defaultFolder 非空时,将 defaultFolder 作为前缀添加到 path Example: ApplyDefaultFolder("note.md", "inbox") => "inbox/note.md" 例如: ApplyDefaultFolder("note.md", "inbox") => "inbox/note.md"

ApplyDefaultFolder("folder/note.md", "inbox") => "folder/note.md" (unchanged)
ApplyDefaultFolder("folder/note.md", "inbox") => "folder/note.md" (不变)
ApplyDefaultFolder("note.md", "") => "note.md" (unchanged)
ApplyDefaultFolder("note.md", "") => "note.md" (不变)

func ArrayUnique

func ArrayUnique(arr []string) []string

ArrayUnique removes duplicate elements from a slice ArrayUnique 移除切片中的重复元素 arr: original slice // 原始切片 return: new slice without duplicates // 返回值: 去重后的新切片

func AuthCodeEncrypt

func AuthCodeEncrypt(token string, action string, key string) (out string, err error)

func AuthDzCodeEncrypt

func AuthDzCodeEncrypt(str, operation, key string, expiry int64) (string, error)

*

  • str: plaintext or ciphertext // 明文或密文
  • operation: encryption ENCODE or decryption DECODE // 加密ENCODE或解密DECODE
  • key: secret key // 密钥
  • expiry: secret key validity period // 密钥有效期

func Ceil

func Ceil(a, b int64) int64

func CheckPasswordHash

func CheckPasswordHash(hash, password string) bool

CheckPasswordHash verifies whether password matches the hash CheckPasswordHash 验证密码与哈希值是否匹配 hash: stored hash value // 存储的哈希值 password: password to be verified // 待验证的密码 return: true if password matches, false otherwise // 返回值: 如果密码匹配返回true,否则返回false

func CopyFile

func CopyFile(src, dst string) error

CopyFile copies a file from src to dst.

func EncodeHash32

func EncodeHash32(content string) string

EncodeHash32 performs 32-bit hash encoding on a string EncodeHash32 对字符串进行 32 位哈希编码

func EncodeMD5

func EncodeMD5(str string) string

EncodeMD5 performs MD5 encoding on a string EncodeMD5 对字符串进行MD5编码 str: string to be encoded str: 待编码的字符串 return: MD5 encoded 32-bit hexadecimal string 返回值: MD5编码后的32位十六进制字符串

func GeneratePasswordHash

func GeneratePasswordHash(password string) (string, error)

GeneratePasswordHash generates bcrypt hash of a password GeneratePasswordHash 生成密码的bcrypt哈希值 password: original password string // 原始密码字符串 return: hashed password string, and possible error info // 返回值: 哈希后的密码字符串,以及可能的错误信息

func GeneratePathVariations

func GeneratePathVariations(path string) []string

GeneratePathVariations generates all suffix variations of a path for backlink matching. Given "projects/test-backlinks/folder-a/note.md", returns: ["note", "folder-a/note", "test-backlinks/folder-a/note", "projects/test-backlinks/folder-a/note"] This allows matching links like [[note]], [folder-a/note], etc. GeneratePathVariations 生成路径的所有后缀变体,用于反向链接匹配。 给定 "projects/test-backlinks/folder-a/note.md",返回: ["note", "folder-a/note", "test-backlinks/folder-a/note", "projects/test-backlinks/folder-a/note"] 这允许匹配类似 [[note]], [folder-a/note] 等链接。

func GenerateRandomNumber

func GenerateRandomNumber(start int, end int, count int) []int

func GenerateRandomSingleNumber

func GenerateRandomSingleNumber(start int, end int) int

GenerateRandomSingleNumber generates a single random number GenerateRandomSingleNumber 生成单个随机数 start: minimum value of random number start: 随机数的最小值 end: maximum value of random number end: 随机数的最大值 return: generated random number 返回值: 生成的随机数

func GetEndTime

func GetEndTime(d time.Time) time.Time

GetEndTime gets 23:59:59 time of a certain day GetEndTime 获取某一天的23:59:59时间 d: given time d: 传入的时间 return: 23:59:59 time of that day 返回值: 当天的23:59:59时间

func GetFirstDateOfMonth

func GetFirstDateOfMonth(d time.Time) time.Time

GetFirstDateOfMonth gets the first day of the month for the given time, which is 0:00 on the first day of the month GetFirstDateOfMonth 获取传入的时间所在月份的第一天,即某月第一天的0点 d: given time d: 传入的时间 return: 0:00 on the first day of that month 返回值: 该月第一天的0点时间

func GetIndexSlice

func GetIndexSlice(arr []string, val string) int

GetIndexSlice gets the index of a slice element GetIndexSlice 获取切片元素的索引 arr: slice to search arr: 待查找的切片 val: value to search for val: 要查找的值 return: index of the element, or -1 if not found 返回值: 元素的索引,如果不存在返回-1

func GetLastDateOfMonth

func GetLastDateOfMonth(d time.Time) time.Time

GetLastDateOfMonth gets the last day of the month for the given time, which is 0:00 on the last day of the month GetLastDateOfMonth 获取传入的时间所在月份的最后一天,即某月最后一天的0点 d: given time d: 传入的时间 return: 0:00 on the last day of that month 返回值: 该月最后一天的0点时间

func GetLastDateOfNextMonth

func GetLastDateOfNextMonth(d time.Time) time.Time

GetLastDateOfNextMonth gets the last day of the next month for the given time GetLastDateOfNextMonth 获取传入时间的下个月最后一天 d: given time d: 传入的时间 return: last day of the next month 返回值: 下个月最后一天的时间

func GetMachineID

func GetMachineID() string

GetMachineID gets unique identifier of the current machine GetMachineID 获取当前机器的唯一标识符 Prioritize machineid library, fallback to motherboard serial number 优先使用 machineid 库,失败则尝试获取主板序列号 return: machine ID string, returns empty string if all failed 返回值: 机器ID字符串,如果全部获取失败则返回空字符串

func GetOSPrettyName

func GetOSPrettyName() string

GetOSPrettyName gets a more readable and detailed OS name and version GetOSPrettyName 获取更具可读性和详细的操作系统名称及版本

func GetRandomString

func GetRandomString(length int) string

GetRandomString generates random string of specified length GetRandomString 生成指定长度的随机字符串

func GetZeroTime

func GetZeroTime(d time.Time) time.Time

GetZeroTime gets 0:00 time of a certain day GetZeroTime 获取某一天的0点时间 d: given time d: 传入的时间 return: 0:00 time of that day 返回值: 当天的0点时间

func InArray

func InArray(nums []int, num int) bool

InArray checks whether an integer is in a slice (used for random number generation) InArray 检查整数是否在切片中(用于随机数生成) nums: integer slice nums: 整数切片 num: integer to be checked num: 待检查的整数 return: true if in slice, false otherwise 返回值: 如果在切片中返回true,否则返回false

func InSlice

func InSlice[T comparable](slice []T, item T) bool

InSlice determines whether an element is in a slice (generic version) InSlice 判断元素是否在切片中(泛型版本) slice: the slice // 切片 item: the element to find // 要查找的元素 return: bool - true if exists, false otherwise // 返回值: bool - 存在返回true,否则返回false

func Inarray

func Inarray(arr []string, val string) bool

Inarray determines whether an element is in a slice Inarray 判断元素是否在切片中 arr: slice to search // 待查找的切片 val: value to search for // 要查找的值 return: true if element is in the slice, false otherwise // 返回值: 如果元素在切片中返回true,否则返回false

func IntSliceToStrSlice

func IntSliceToStrSlice(list []int) []string

IntSliceToStrSlice converts integer slice to string slice (another implementation) IntSliceToStrSlice 将整数切片转换为字符串切片(另一种实现) list: integer slice // 整数切片 return: string slice // 返回值: 字符串切片

func IntSliceToStringSlice

func IntSliceToStringSlice(intSlice []int) []string

IntSliceToStringSlice converts integer slice to string slice IntSliceToStringSlice 将整数切片转换为字符串切片 intSlice: integer slice // 整数切片 return: string slice // 返回值: 字符串切片

func IntersectionInt

func IntersectionInt(a, b []int) []int

IntersectionInt calculates intersection of two integer slices IntersectionInt 计算两个整数切片的交集 a: first integer slice // 第一个整数切片 b: second integer slice // 第二个整数切片 return: intersection of two slices // 返回值: 两个切片的交集

func IsValidEmail

func IsValidEmail(email string) bool

IsValidEmail verifies if the email format is correct IsValidEmail 验证邮箱格式是否正确 email: email string to be verified email: 待验证的邮箱字符串 return: true if email format is correct, false otherwise 返回值: 如果邮箱格式正确返回true,否则返回false

func IsValidUsername

func IsValidUsername(username string) bool

IsValidUsername verifies if the username format is correct IsValidUsername 验证用户名格式是否正确 username: username to be verified username: 待验证的用户名 return: true if username format is correct, false otherwise 返回值: 如果用户名格式正确返回true,否则返回false

func MergeFrontmatter

func MergeFrontmatter(existing, updates map[string]interface{}, removeKeys []string) map[string]interface{}

MergeFrontmatter merges updates into existing frontmatter and removes specified keys MergeFrontmatter 将更新合并到现有的 frontmatter 中并移除指定的键

func MoveFile

func MoveFile(src, dst string) error

MoveFile moves a file from src to dst, supporting cross-device move. MoveFile 将文件从 src 移动到 dst,支持跨设备移动。

func ParseDuration

func ParseDuration(s string) (time.Duration, error)

ParseDuration parses duration string, supports 'd' (day) suffix ParseDuration 解析时间字符串,支持 'd' (天) 后缀

func ParseFrontmatter

func ParseFrontmatter(content string) (yamlData map[string]interface{}, body string, hasFrontmatter bool)

ParseFrontmatter extracts YAML frontmatter from content Returns the parsed YAML as a map, the body (content after frontmatter), and whether frontmatter exists ParseFrontmatter 从内容中提取 YAML frontmatter 返回解析后的 YAML map、正文(frontmatter 之后的内容)以及是否存在 frontmatter

func ParseSize

func ParseSize(sizeStr string, defaultSize int64) int64

ParseSize parses size string like "128MB", "512KB", "1024B" to bytes ParseSize 将大小字符串(如 "128MB", "512KB", "1024B")解析为字节数

func ReconstructContent

func ReconstructContent(yamlData map[string]interface{}, body string) string

ReconstructContent rebuilds content with frontmatter ReconstructContent 使用 frontmatter 重新构建内容

func RemoveDuplicate

func RemoveDuplicate(strSlice []string) []string

RemoveDuplicate removes duplicate elements from a string slice (another implementation) RemoveDuplicate 移除字符串切片中的重复元素(另一种实现) strSlice: original string slice // 原始字符串切片 return: string slice without duplicates // 返回值: 去重后的字符串切片

func StrToInt

func StrToInt(str string) int

StrToInt converts string to integer StrToInt 将字符串转换为整数 str: string to be converted // 待转换的字符串 return: converted integer, or 0 if conversion fails // 返回值: 转换后的整数,如果转换失败返回0

func StrToMap

func StrToMap(str string) map[string]string

StrToMap converts string to map StrToMap 将字符串转换为map str: string in the format of "key=value,key=value" // 格式为"key=value,key=value"的字符串 return: converted map // 返回值: 转换后的map

func StringToInt64

func StringToInt64(s string) int64

StringToInt64 converts string to int64 StringToInt64 将字符串转换为int64 s: string to be converted // 待转换的字符串 return: converted int64 value // 返回值: 转换后的int64值

func TimeParse

func TimeParse(layout string, in string) time.Time

TimeParse time and date formatting TimeParse 时间日期格式化 layout: time format layout: 时间格式 in: time string to be parsed in: 要解析的时间字符串 return: parsed time object 返回值: 解析后的时间对象

func ValidatePath

func ValidatePath(path string) bool

ValidatePath checks if a path is safe (no directory traversal). Returns true if the path is valid, false if it contains "..". ValidatePath 检查路径是否安全(无目录遍历)。 如果路径有效则返回 true,如果包含 ".." 则返回 false。

func Wait

func Wait(num float32)

Wait waits for specified number of seconds Wait 等待指定的秒数 num: number of seconds to wait num: 等待的秒数

func WhoCalled

func WhoCalled() string

func XorEncodeStr

func XorEncodeStr(msg []byte, key []byte) (out []byte)

XorEncodeStr encrypts a byte slice using XOR operation XorEncodeStr 使用异或操作对字节切片进行加密 msg: byte slice to be encrypted // 要加密的字节切片 key: key byte slice // 加密密钥的字节切片 return: encrypted byte slice // 返回值: 加密后的字节切片

func XorEncodeStrRune

func XorEncodeStrRune(msg []rune, key []rune) (out []rune)

XorEncodeStrRune encrypts a rune slice using XOR operation XorEncodeStrRune 使用异或操作对rune切片进行加密 msg: rune slice to be encrypted // 要加密的rune切片 key: key rune slice // 加密密钥的rune切片 return: encrypted rune slice // 返回值: 加密后的rune切片

func Zip

func Zip(source, target string) error

Zip compresses files or directories into a zip file source: path to file or directory target: path to output zip file

func ZipBytes

func ZipBytes(files map[string][]byte, target string) error

ZipBytes creates a zip archive from a map of filenames and their contents (bytes)

Types

type WikiLink struct {
	Path    string // The target path // 目标路径
	Alias   string // Optional alias from [[link|alias]] // 可选别名
	IsEmbed bool   // True if this is an embed (![[...]]) rather than a link ([[...]]) // 是否为嵌入 (![[...]])
}

WikiLink represents a wiki-style link extracted from content // WikiLink 表示从内容中提取的维基风格链接

func ParseWikiLinks(content string) []WikiLink

ParseWikiLinks extracts [[wiki-links]], [[link|alias]], and ![[embeds]] from content Returns a slice of WikiLink with path, optional alias, and embed flag ParseWikiLinks 从内容中提取 [[wiki-links]], [[link|alias]], 和 ![[embeds]] 返回包含路径、可选别名和嵌入标记的 WikiLink 切片

Jump to

Keyboard shortcuts

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