您的位置 首页 golang

「Golang」将静态文件打包到二进制

前言:经常写java应用,大家都知道的是打成jar包,把静态文件都直接最后以一个xxx.jar包展现。在运行的时候也特别方便。那么go其实也有这样的操作。

「Golang」将静态文件打包到二进制

了解目前开源的包

  • go-bindata
  • packcr
  • statik

目前是这三种

推荐statik进行开发

流程清晰明了,基本上看一眼就可以用。

其实核心思想都是将静态文件转换为.go文件,然后再通过import将包导入进去

开始使用

 #安装 
go get github.com/rakyll/statik
#查看安装结果
statik --help  
  • 建立test文件夹,在文件夹内分别建立 main.go和a.json
 #main.go
package main

import (
	_ "./statik"  //注意该包,该包非手动创建,由static命令进行创建
	"fmt"
	"github.com/rakyll/statik/fs"
	"io/ioutil"
	"log"
)

func main() {
	staticFile, err := fs.New()
	if err != nil {
		log.Fatal(err)
	}
	f, err := staticFile.Open("/a.json")
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()
	contents, err := ioutil.ReadAll(f)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(string(contents))
}
  
 #a.json
{"a": 1}  
  • static命令,将静态文件生成.go的包文件
 #执行static的静态文件转换命令
static -src=./ -include=a.json
# -src默认是public目录
# -include 如果不写,默认目录全部文件
# 执行完上述命令会在当前文件夹产生 static/static.go 此文件就是main.go的注释部分  
  • 编译
 #在当前命令行
go build
ls
#执行可执行文件
./test
#结果
{"a": 1}  
  • 测试
 #将可执行文件移动到其它地方
mv test /xxxx
#执行
./test
#结果
{"a": 1}  

到此就结束了。核心思想在推荐的时候描述了。

总结

快速解决自己想要的,之前使用packcr,这个包宣传的多么好,结果使用起来问题非常多,果断换之。

参考资料:

文章来源:智云一二三科技

文章标题:「Golang」将静态文件打包到二进制

文章地址:https://www.zhihuclub.com/97573.shtml

关于作者: 智云科技

热门文章

评论已关闭

28条评论

  1. Usual ductal hyperplasia without atypia Fibroadenoma Sclerosing adenosis Several papillomas called papillomatosis Radial scar

  2. Rimm, David L This study indicated reduced association of DualL in the presence of free transferrin p

  3. The cell suspension was applied onto the column that was then rinsed with 3 500 ОјL PBS buffer to allow CD14 cells to pass through

  4. We propose that under normal conditions, RhoBTB1 tightly regulates the activity of PDE5 by ensuring that excess PDE5 is targeted for ubiquitination and proteasomal degradation see model in Figure 8N There were dynamic changes in all three layers of E13

  5. However, don t cut out fats abruptly or eliminate them altogether, as too little fat can also result in gallstone formation It is important to note that not all PMS symptoms are caused by progesterone deficiency and estrogen dominance

  6. However, this maneuver failed to enhance the activity of NKCC2 A, B, or F isoforms above that of endogenously expressed NKCC1, despite higher baseline activity caused by overexpression of either NKCC2 isoform

  7. a disorder with includes symptoms like hearing, seeing or sensing things that are not real, mistaken beliefs and thoughts, and problems dealing with other people

  8. Usually, a retailer will provide testing data they get from a third party service Peptides Warehouse does not D Spleen weights of moribund DKO MDS mice n 6 and their controls WT, n 5; О”E9 10, n 5; О”Tet2, n 5

  9. Putting me a bad experience repeated ultrasonic observation of ttc baby dust However, tamoxifen does not seem to be an effective treatment for benign mammary tumors

  10. Four representative calcium waves were color coded red, orange, yellow, and green and superimposed to display the spatial expansion of calcium waves in control vehicle treated cultures C and tamoxifen exposed cultures D; 2 h, 10 Ој m

  11. He was compelled by sharia, he said, to choose the lesser of two evils in supporting early elections

  12. Interestingly, cell cycle regulatory proteins, p21 and p27, were down regulated upon ACAT 1 inhibition In contrast, incidence rates for hip fractures double every 5 years after age 70 Fig

  13. PatanГЁ M, Ciriaco M, Chimirri S, Ursini F, Naty S, Grembiale RD, Gallelli L, De Sarro G, Russo E

  14. 22 Jaferi A, Bhatnagar S Corticosterone can act at the posterior paraventricular thalamus to inhibit hypothalamic pituitary adrenal activity in animals that habituate to repeated stress currently 4dpo

  15. I completed two years on Tamoxifen and then took a break from it to try and conceive another baby Contact your insurance provider or healthcare provider to determine the exact cost of Nexplanon or Mirena

  16. Tanning of the skin Materials and Methods 144 women with unexplained or male factor infertility undergoing IUI cycles were randomized 72 patients in CC plus rFSH group and 72 patients in CC plus HMG group and included in this single blind study from October 2006 to June 2010

  17. There are thousands upon thousands of drugs and alternative studies so it would be humanly impossible to list them all, although I did try

  18. zerit tadalafila 5mg com 28 comprimidos sandoz genrico The report stressed medical science is not yet able to offer radical life extension treatment and noted that three quarters of those polled did not think average people in 2050 would be able to live to 120 or longer

网站地图