将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?

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: