I would like to wrap a data file (~1MB) to golang app and then use that data in os.exec
. The app runs on Linux.
How to define the data in the app, as a string
or []byte
, variable, or Const?
Should be defined in a global scope, or wrapped in a func
?
How to pass the data from the app memory to the executed process ?
For building the data file(s) in to your program, you have a number of choices. You are correct in that you could manually copy/paste the data file(s) in to the program as types string
, []byte
as variables, but there are other packages/applications for go that handle this for you already that can also minimize your app's memory footprint. One example that comes to mind is go-bindata
(link to repo) which does just this.
As for using the file in os/exec
, I'm assuming you're trying to either pass the entire file to the program using either a file path or a raw string. For file paths, you would have to write the entire file to disk first. If you're trying to pass the entire file contents as a raw string, you can still use go-bindata
or a string
of the data file as arg
in os/exec.Command
.
P.S. - go-bindata
has not seen updates in a while, so I would encourage you to seek more active forks or alternatives if you're having trouble using it.