Golang-将文件系统嵌入二进制文件; 与库一起使用
I'm using go.rice to store an embedded filesystem in the Go binary. This works perfectly for pushing to production (i.e. single binary distribution), and keeps all 'file' access in memory.
The issue I'm running into is third-party libs that are designed to load external files from the filesystem directly by passing in a file name string (rather than, say, a generic Reader interface that would have allowed me to abstract file loading)
Is there some way to create an in-memory filesystem that works with Go's standard library os/io tools and therefore bypass the need to store assets outside of the binary?
I could dump the bytes to a tmp file, pass that to the libs, and then delete... but that seems messy. Would prefer to keep access in memory, if possible.
Doesn't have to be go.rice... any other embedding mechanisms to keep this clear/single file distribution?
我正在使用go.rice将嵌入式文件系统存储在Go二进制文件中。 这非常适合推送到生产环境(即单个二进制分发),并将所有“文件”访问权限保留在内存中。 p>
我遇到的问题是设计的第三方库 可以通过传入文件名字符串直接从文件系统加载外部文件(而不是一般的Reader接口,该接口允许我抽象文件加载) p>
创建一个可以与Go的标准库os / io工具一起使用的内存文件系统,从而绕开了在二进制文件之外存储资产的需要? p>
我可以将字节转储到tmp文件中 ,将其传递给库,然后删除...,但这似乎很混乱。 p>
不必一定要保留在内存中。ric...其他任何嵌入机制可以保持这种清晰/单一的文件分发? p > div>
I'm sorry, that is not possible. The os
package is a thin wrapper atop the platforms operating system API. What you could do is this:
- Use
go.rice
to create an embedded file system in your binary. - At runtime: copy the embedded file to a temporary location (you can use
os.TempDir()
to get a suitable place to make your temporary directory in) - Use the temporary path to pass file names to third-party code.
- On program exit, clean up the temporary directory.
Alternatively, you could request the developers of the third-party code to include an extra API that accepts an io.Reader
instead of a file name.