Go:用bindata嵌入JS文件 [英] Go: embed JS files with bindata
问题描述
这个问题跟在我之前的一个问题上。我已经关闭了这个问题,所以我希望能够在这里提出一个新的但相关的问题。 Go:以二进制格式嵌入静态文件
如何使用go-bindata提供JS文件?我将它传递到html这样的
hi.html
<脚本> {{Bindata}}>< /脚本>
即使我没有编译或JS错误,它似乎也不起作用。 b $ b
使用 https: 假设你有以下结构: b 其中 go-bindata-assetfs 一个>工具非常简单。它将查看您传递给它的目录,并生成一个包含可在这些文件中包含二进制数据的变量的源文件。因此,请确保您的静态文件存在,然后从 现在,默认情况下,这会在包中创建一个源文件 在在这种情况下,生成的文件 在我的应用程序中,我已经有了一些代码来处理目录中的文件: 确保类似这样的东西正常工作,首先。然后将FileServer行更改为: 现在你有一个生成的源文件,其中包含你的静态资源。您现在可以安全地删除'ui'子目录结构。编译 你应该有一个二进制文件,它可以正确地为你的静态资产提供服务。 This question is a follow up to an earlier question of mine. I've closed the question so I hope its okay that I ask a fresh but related question here. Go: embed static files in binary How do I serve JS files with go-bindata? Do I pass it into html like this hi.html Doesn't seem to work even though I have no compile or JS errors. Using https://github.com/elazarl/go-bindata-assetfs Assuming you have the following structure: Where The go-bindata-assetfs tool is pretty simple. It will look at the directories you pass to it and generate a source file with variables that can contain the binary data in those files. So make sure your static files are there, and then run the following command from Now, by default, this will create a source file in the package In this case, the generated file In my app, I already had some code that served files from a directory: Make sure something like this works properly, first. Then it's trivial to change the FileServer line to:
Now you have a generated source file with your static assets in them. You can now safely remove the 'ui' subdirectory structure. Compile with And you should have a binary that serves your static assets properly. 这篇关于Go:用bindata嵌入JS文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
myprojectdirectory
├───api
├───cmd
├───datastores
└────ui
├───css
└───js
ui
是您想要打包并打包到您的应用程序中的目录结构...
生成源文件
myprojectdirectory
运行以下命令:
$ b go-bindata-assetfs ./ui /...
主
。有时候,这没关系。就我而言,事实并非如此。如果您愿意,可以使用不同的软件包名称生成文件:
$ b go-bindata-assetfs.exe -pkg cmd ./ui /...
将源文件放在正确的位置
bindata_assetfs.go
会在 myprojectdirectory 目录中创建(这是不正确的)。在我的情况下,我只是手动将文件移动到 cmd 目录。
更新您的应用程序代码
import(
net / http
github.com/gorilla/mux
)
//创建路由器并设置路由
var Router = mux。 NewRouter()
Router.PathPrefix(/ ui)。Handler(http.StripPrefix(/ ui,http.FileServer(http.Dir(./ ui))))
//开始监听
http.ListenAndServe(127.0.0.1:3000,Router)
$ p $ Router.PathPrefix(/ ui)。Handler(http.StripPrefix( / ui,http.FileServer(assetFS())))
编译应用程序
go install ./...
<script>{{.Bindata}}></script>
myprojectdirectory
├───api
├───cmd
├───datastores
└───ui
├───css
└───js
ui
is the directory structure you'd like to wrap up and pack into your app...Generate a source file
myprojectdirectory
:go-bindata-assetfs ./ui/...
main
. Sometimes, this is ok. In my case, it isn't. You can generate a file with a different package name if you'd like:go-bindata-assetfs.exe -pkg cmd ./ui/...
Put the source file in the correct location
bindata_assetfs.go
is created in the myprojectdirectory directory (which is incorrect). In my case, I just manually move the file to the cmd directory.Update your application code
import (
"net/http"
"github.com/gorilla/mux"
)
// Create a router and setup routes
var Router = mux.NewRouter()
Router.PathPrefix("/ui").Handler(http.StripPrefix("/ui", http.FileServer(http.Dir("./ui"))))
// Start listening
http.ListenAndServe("127.0.0.1:3000", Router)
Router.PathPrefix("/ui").Handler(http.StripPrefix("/ui", http.FileServer(assetFS())))
Compile the app
go install ./...