https://github.com/fcjr/lazyexe
Lazily load embedded executables in go.
https://github.com/fcjr/lazyexe
embed executable go goembed golang lazy-loading
Last synced: about 2 months ago
JSON representation
Lazily load embedded executables in go.
- Host: GitHub
- URL: https://github.com/fcjr/lazyexe
- Owner: fcjr
- License: mit
- Created: 2021-03-22T12:36:33.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2021-09-16T14:47:22.000Z (over 4 years ago)
- Last Synced: 2024-06-20T00:28:06.010Z (almost 2 years ago)
- Topics: embed, executable, go, goembed, golang, lazy-loading
- Language: Go
- Homepage:
- Size: 34.2 KB
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# LazyExe
Lazily write an executable to disk. Useful for embedding executables via go's new `//go:embed` directive.
A new LazyExe can be created by running `lazyexe.New(exeBytes)`. Don't call `Path()` until you need to run the embedded executable.
The executable is written out to your tmp folder and chmoded the first time the `Path()` is requested. Subsequent calls to `Path()`
will return the same executable. When finished using the executable you are expected to call `Cleanup()` to ensure the temporary
file is removed from the system.
## Example
Try running `example/main.go` which embeds a [cosmopolitan libc](https://justine.lol/cosmopolitan/index.html) hello world, loads it, and runs it!
```go
package main
import (
_ "embed"
"fmt"
"log"
"os/exec"
"github.com/fcjr/lazyexe"
)
//go:embed hello.com
var helloworld []byte
func main() {
exe := lazyexe.New(helloworld)
defer exe.Cleanup()
exePath, err := exe.Path()
if err != nil {
log.Fatal(err)
}
out, err := exec.Command(exePath).CombinedOutput()
if err != nil {
log.Fatal(err)
}
fmt.Print(string(out))
}
```