0

I am reading some data from a YAML file inside my go code:

func main() {
  yamlFile, err := ioutil.ReadFile("data.yaml")
  if err != nil {
    fmt.Println("Failed to open grid map file with err: ", err.Error())
    return

  // ... do something with the data from the yamlFile variable
}

However, when I make the binary using the go build command, the YAML file does not get packaged into the binary file. Is there any way to package/embed it in the binary file using go 1.15? I do not want to use any CLI tools like statik, packr, pkger, etc as I have to incorporate it in an existing pipeline and that too without go generate.

I know that this can be done in go 1.16 using the embed package. But I have to use go 1.15.

The data in the YAML file will be provided by the user. Hence, hard coding is not an option.

The software will read the data from the YAML file and then the user can build the executable binary file using Docker to be deployed on their own hardware.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
MRA1412
  • 57
  • 8
  • The top voted answer in the marked duplicate also lists your old options (prior to Go 1.16). – icza May 16 '22 at 12:40
  • The problem is that the YAML file will be provided by the user. If the data was known then I would've hard-coded it. – MRA1412 May 16 '22 at 16:57
  • 2
    Your requirements are contradictory. Either you embed the file in the executable or allow users to provide it (possibly from a fixed location relative to the workdir). You can't have both, unless you ask your users to build the executable themselves – blackgreen May 16 '22 at 20:56
  • Basically, the user will provide their own YAML file and the go code will be deployed using Docker. Essentially the users are building their own executable. – MRA1412 May 17 '22 at 04:59
  • @MRA1412 If you want it to be packed with the binary using Go prior to 1.16, the YAML source must appear in the Go source code, e.g. as initializing a variable with it. You must generate a Go source file from the YAML file. – icza May 17 '22 at 07:09

0 Answers0