douhu2890 2018-02-24 16:29
浏览 43

将Go结构数据嵌入二进制

I have several hundred records of metadata I need available to my Go program. I would like the data to be compiled into the binary so it just works, avoiding dependencies on additional files to run.

The metadata starts as csv, and I have a another package that processes the csv into structs. The main program uses these structs by importing that package. The csv only changes occasionally.

I learned how to import and export the structs as gobs, but I would need to store those gobs in a file (etc) and load them in at runtime.

I looked at making shared libraries, but those are in ELF format and don't seem to support the native Go structs I need.

I could have the csv-processing package literally print out go code to recreate these struct instances (with say a base64-encoded gob of it), to be built with the main program, but that seems like an awfully roundabout way to go about this.

Is there a simple way to embed this prepared struct data into my binary?

  • 写回答

1条回答 默认 最新

  • douruduan8812 2018-02-24 18:41
    关注

    There are a little bit packages which make possible to embed resource (binaries) into your app.

    I was using the following:

    • I choose go-resources to use in production. It generates go source code file contained your data as hex encoded;
    • another useful package: go.rice - is real embed your data as a resource into an executable file. But I got some issue for cross-platform building;

    I find packages wich doing the same task, but I didn't use it in production:

    评论

报告相同问题?

悬赏问题

  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思