How to embed files into Golang binaries?

后端 未结 7 1336
遇见更好的自我
遇见更好的自我 2020-12-12 16:58

I have some text file that I read from my Go program. I\'d like to ship a single executable, without supplying that text file additionally. How do I embed it into compilatio

7条回答
  •  攒了一身酷
    2020-12-12 17:38

    I used a simple function to read an external template in a go generate run and to generate Go code from it. A function returning the template as a string will be generated. One can then parse the returned template string using tpl, err := template.New("myname").Parse(mynameTemplate())

    I did put that code to github. You might want to try https://github.com/wlbr/templify

    Very simple, but works for me quite well.

提交回复
热议问题