如何嵌入文件以供以后解析执行使用 [英] How to embed file for later parsing execution use

查看:64
本文介绍了如何嵌入文件以供以后解析执行使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实质上是试图浏览html文件的文件夹.我想将它们嵌入到二进制文件中,并能够根据请求解析它们以用于模板执行目的. (如果我的措词不正确,请原谅.)

I am essentially trying to walk through a folder of html files. I want to embed them into the binary file and be able to parse them upon request for template execution purposes. (Please excuse me if im not wording this properly).

任何实现此目的的想法,技巧,窍门或更好的方法都将受到赞赏.

Any ideas, tips, tricks or better way of accomplishing this is much appreciated.

// Template Files
type TempFiles struct {
    Files map[string]string
}

// Loop through view files and load them
func LoadTempFiles() {
    t := new(TempFiles)

    // Load template files
    filepath.Walk("application/views", func(path string, info os.FileInfo, err error) error {
    if !info.IsDir() {
        content, _ := ioutil.ReadFile(path)
        t.Files[path] = string(content)
    }
    return nil
    })
}

func ViewTemp(w http.ResponseWriter, path string) {
    t := new(TempFiles)

    temp, err := template.New().Parse(t.Files[path])
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
    } else {
        temp.Execute(w, nil)
    }
}

推荐答案

我对大多数Go Web应用程序都执行此操作.我使用 go-bindata 从我想要的所有文件中自动生成Go源代码嵌入,然后将它们编译为二进制文件. 所有这些都是在构建过程中自动完成的.

I do this with most of my Go web apps. I use go-bindata to auto-generate Go source code from all the files I want to embed and then compile them into the binary. All this is done automatically during build.

一个缺点是当前的go构建工具没有提供一种连接构建过程的方法,因此我为此使用了一个Makefile.调用makefile时,它将运行go-bindata来生成所有必需文件的源,然后通常执行一些其他代码生成位和鲍勃(特别是创建一个Go源文件,该源文件列出了映射中的所有嵌入式文件.)目录(如果需要).然后继续编译实际程序.

One downside is that the current go build tools do not offer a way to hook into the build process, so I use a Makefile for this purpose. When the makefile is invoked, it runs go-bindata to generate the sources for all necessary files, then usually performs some additional code generation bits and bobs (notably, creating a Go source file which lists all the embedded files in a map.. A Table of Contents if you will). It then proceeds to compile the actual program.

这可能会有点混乱,但是您只需将其设置一次即可. 另一个缺点是,使用Makefile意味着该软件与go get命令不兼容.但是,由于我的大多数Web应用程序都不打算共享,因此到目前为止,这还不是问题.

This can become a little messy, but you only have to set it all up once. Another downside, is that the use of a Makefile means the software is not compatible with the go get command. But since most of my web apps are not meant to be shared anyway, this has not been a problem so far.

在调试/开发这样的应用程序时,嵌入静态Web内容会引起另一个问题:我不能只是编辑HTML或CSS文件并刷新浏览器以查看其效果.我将不得不停止服务器,重建它,并在每次编辑时重新启动它.这显然是不理想的,因此我将Makefile分为debugrelease模式.释放模式执行我上面描述的操作.但是,调试模式实际上不会嵌入静态文件.它确实会为每个文件生成源文件,但是它不是包含实际文件数据,而是包含存根(stub),该存根仅从文件系统中加载数据.

When it comes to debugging/developing such an application, there is another issue that arises from embedding the static web content: I can't just edit an HTML or CSS file and refresh the browser to see its effects. I would have to stop the server, rebuild it and restart it with every edit. This is obviously not ideal, so I split the Makefile up into a debug and release mode. The release mode does what I described above. The debug mode, however, wil not actually embed the static files. It does generate source files for each of them, but instead of having them contain the actual file data, it contains a stub which simply loads the data from the filesystem.

就服务器代码而言,生成的代码没有区别.它所做的只是调用一个函数来获取给定静态文件的内容.不管该内容是否实际上嵌入在二进制文件中,还是从外部源加载的内容,都无关紧要.因此,这两种构建模式可以自由互换.

As far as the server code is concerned, there is no difference in the generated code. All it does is call a function to fetch the contents of a given static file. It does not care whether that content is actually embedded in the binary, or if it's loaded from an external source. So the two build modes are freely interchangeable.

例如,在释放和调试模式下获取静态文件内容的相同生成函数将如下所示:

For example, the same generated function to fetch static file content in release and debug mode would look as follows:

发布模式:

func index_html() []byte {
    return []byte {
        ....
    }
}

调试模式:

func index_html() []byte {
   data, err := ioutil.ReadFile("index.html")
   ...
   return data
}

两种情况下的界​​面都是相同的.这样可以轻松,轻松地进行开发和调试.

The interface in both cases is identical. This allows for easy and care-free development and debugging.

这篇关于如何嵌入文件以供以后解析执行使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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