Golang 从内存中提供静态文件 [英] Golang serve static files from memory

查看:32
本文介绍了Golang 从内存中提供静态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于在 Go 中提供文件的快速问题.FileServer 处理程序可以节省大量时间,但对于我的用例,我的应用程序只有 2 或 3 个文件(js 和 css),我不想使部署复杂化而不得不考虑这些.

I have a quick question about serving files in Go. There is the great timesaving FileServer handler, but for my use case, I only have 2 or 3 files (js and css) that go with my app and I dont want to complicate the deployment to have to think about those.

您认为有一种简单的方法可以将这两个文件构建到二进制文件中并从那里提供它们.例如,base64 将文件的数据编码为常量,并从常量中提供文件.这将以其最简单的形式工作,但我不想经历文件服务器所做的一切(标头、到期、mime 类型等)的痛苦.那么是否有一种简单的方法可以将这些静态文件以某种形式烘焙到二进制文件中并以这种方式提供服务?

Do you think there is an easy way to build those couple of files into the binary and serve them from there. For example base64 encode the data of the files as constants and server the files from the constants. This would work in its most simple form, but I dont want to go through the pain of doing everything that a file server does (headers, expiries, mime-types, etc) on my own. So would there be an easy way to bake those static files into the binary in some form and serve them that way?

推荐答案

go.rice"包负责为您提供这些 - 在您的二进制文件中嵌入资源,并提供 http.FileSystem 实现.

The "go.rice" package takes care of this for you - embedding resources in your binaries, and providing an http.FileSystem implementation.

这篇关于Golang 从内存中提供静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆