From 39a806695e0d521578d851bf2621deaadc9e09a1 Mon Sep 17 00:00:00 2001 From: Jellyfish Date: Mon, 21 Apr 2025 10:07:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0LoadContentFromFS=E5=87=BD?= =?UTF-8?q?=E6=95=B0=EF=BC=8C=E6=8F=90=E4=BE=9B=E4=BA=86=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E9=80=9A=E7=94=A8=E7=9A=84=E6=96=B9=E6=B3=95=E6=9D=A5=E4=BB=8E?= =?UTF-8?q?=20embed.FS=20=E4=B8=AD=E5=8A=A0=E8=BD=BD=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=86=85=E5=AE=B9=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- binding/golang/xdb/util.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/binding/golang/xdb/util.go b/binding/golang/xdb/util.go index b02dc80..d3b6019 100644 --- a/binding/golang/xdb/util.go +++ b/binding/golang/xdb/util.go @@ -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 +}