mirror of
https://github.com/lionsoul2014/ip2region.git
synced 2025-12-08 19:25:22 +00:00
增加LoadContentFromFS函数,提供了一个通用的方法来从 embed.FS 中加载文件内容。
This commit is contained in:
parent
06eb5c6e82
commit
39a806695e
@ -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
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user