Skip to content

Instantly share code, notes, and snippets.

@ubogdan
Forked from itaysk/Makefile
Created February 12, 2024 10:36
Show Gist options
  • Save ubogdan/bfd515e8c91d5ac89a2372cf1b7aeb26 to your computer and use it in GitHub Desktop.
Save ubogdan/bfd515e8c91d5ac89a2372cf1b7aeb26 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