Giter Site home page Giter Site logo

binclude's Introduction

binclude

GoDoc Test Go Report Card codecov

binclude is a tool for including static files into Go binaries.

  • focuses on ease of use
  • the bincluded files add no more than the filesize to the binary
  • uses ast for typesafe parsing and code generation
  • each package can have its own binclude.FileSystem
  • binclude.FileSystem implements the http.FileSystem interface
  • ioutil like functions FileSystem.ReadFile, FileSystem.ReadDir
  • include all files/ directories under a given path by calling binclude.Include("./path")
  • high test coverage
  • supports execution of executables directly from a binclude.FileSystem (os/exec wrapper)
  • optional compression of files with gzip binclude -gzip or brotli binclude -brotli

Install

go get -u github.com/lu4p/binclude/cmd/binclude

Usage

package main

//go:generate binclude

import (
	"io/ioutil"
	"log"

	"github.com/lu4p/binclude"
)

func main() {
	binclude.Include("./assets")
	binclude.Include("file.txt")

	f, err := BinFS.Open("file.txt")
	if err != nil {
		log.Fatalln(err)
	}

	out, err := ioutil.ReadAll(f)
	if err != nil {
		log.Fatalln(err)
	}

	log.Println(string(out))

	infos, err := BinFS.ReadDir("./assets")
	if err != nil {
		log.Fatalln(err)
	}

	for _, info := range infos {
		log.Println(info.Name())
	}
}

To build use:

go generate
go build

Binary size

The resulting binary can get quite large, with the included files. You can add compression of the included files with -gzip or -brotli.

Note: decompression is optional to allow for the scenario where you want to serve compressed files for a webapp directly.

You can reduce the final binary size by building without debug info (go build -ldflags "-s -w") and compressing the resulting binary with upx (upx binname).

Note: If you don't need to access the compressed form of the files I would advise to just use upx and don't add seperate compression to the files.

OS / Arch Specific Includes

Binclude supports including files/binaries only on specific architectures and OS. Binclude follows the same pattern as Go's Build Constraints and will ignore files that contain the suffixes:

*_GOOS
*_GOARCH
*_GOOS_GOARCH

If they do not match the current GOOS / GOARCH.

For example, if you want to include a binary only on Windows you could have a file static_windows.go and reference the static file:

package main

import "github.com/lu4p/binclude"

func bar() {
  binclude.Include("./windows-file.dll")
}

binclude's People

Contributors

audiolion avatar lu4p avatar renovate-bot avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.