增加LoadContentFromFS函数,提供了一个通用的方法来从 embed.FS 中加载文件内容。

This commit is contained in:
Jellyfish 2025-04-21 10:07:38 +08:00
parent 06eb5c6e82
commit 39a806695e

View File

@ -9,6 +9,7 @@
package xdb
import (
"embed"
"fmt"
"os"
"strconv"
@ -180,3 +181,21 @@ func LoadContentFromFile(dbFile string) ([]byte, error) {
return cBuff, nil
}
// LoadContentFromFS 提供了一个通用的方法来从 embed.FS 中加载文件内容
func LoadContentFromFS(fs embed.FS, filePath string) ([]byte, error) {
file, err := fs.Open(filePath)
if err != nil {
return nil, fmt.Errorf("failed to open embedded file `%s`: %w", filePath, err)
}
defer file.Close()
// 读取文件内容到字节数组
var cBuff []byte
cBuff, err = io.ReadAll(file)
if err != nil {
return nil, fmt.Errorf("failed to read embedded file `%s`: %w", filePath, err)
}
return cBuff, nil
}