Skip to content

Instantly share code, notes, and snippets.

@itaysk
Created April 29, 2020 10:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save itaysk/993329ef49a4fee26fbb57be6bc28e32 to your computer and use it in GitHub Desktop.
Save itaysk/993329ef49a4fee26fbb57be6bc28e32 to your computer and use it in GitHub Desktop.
Embed file in Go using ELF section
package main
import (
"debug/elf"
"fmt"
"os"
)
func main() {
selfPath, _ := os.Executable()
selfFile, _ := os.Open(selfPath)
selfElf, _ := elf.NewFile(selfFile)
var data []byte
for _, s := range selfElf.Sections {
if s.Name == "myfile" {
data, _ = s.Data()
}
}
fmt.Println(string(data))
}
build:
go build -o _main
objcopy --add-section myfile=myfile _main main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment