embedfiles

command module
v0.0.0-...-22be4a4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 20, 2016 License: MIT Imports: 12 Imported by: 0

README

Embed files into Go source code

The embedfiles command converts one or more files into Go source code so that they may be compiled directly into a Go program.

It is intended to be run via go generate tool and creates an instance variable that provides file-like access to the embedded assets using a bytes.Reader.

Additionally each file instance complies with the http.File interface.

By default the generated package does not provide a type compatible with the http.FileServer interface to avoid importing net/http - Supplying the -include-http flag will enable support for that interface.

For example to embed the html and css files in an assets directory into a new file called assets.go as part of the main package (use the -package option to generate a file in a different pacakge)

embedfiles -filename assets.go -include-http -var Assets assets/*.html assets/*.css

index.html could then be opened for read:

f, err := Assets.Open("assets/index.html")

or Assets could be used with an instance as a file server (assuming the -include-http flag was set):

log.Fatal(http.ListenAndServe(":8080", http.FileServer(Assets))

As each call to embedfiles generates a completely self-contained .go file, multiple independent .go files can be generated and compiled into a single package by using different -varname options, allowing for discrete groups of files to be assigned to different variable names.

Usage

  Usage:  embedfiles [arguments] <file glob> [<file glob> ...]

  Arguments:

    -filename string
          File to write go output to.  Defaults to stdout (default "-")
    -include-http
          If true then the generated file will import net/http and comply with the http.FileSystem interface
    -package string
          Package name to use for output file. (default "main")
    -var string
          Variable name to assign the assets to.  Start with a capital letter to export from the package (default "assets")

Usage with go generate

Add one or more lines to existing .go source code to trigger embedfiles when go generate is executed:

//go:generate embedfiles -filename email_templates.go templates/*.txt

(note the lack of spaces between // and go:generate there!)

See the go generate blog post or documentation for more details

Documentation

Overview

Command embedfiles converts one or more files into Go source code so that they may be compiled directly into a Go program.

It is intended to be run via the go generate tool and creates an instance variable that provides file-like access to the embedded assets using a bytes.Reader.

Additionally each file instance complies with the http.File interface.

By default the generated package does not provide a type compatible with the http.FileServer interface to avoid importing net/http - Supplying the -include-http flag will enable support for that interface.

For example to embed the html and css files in an assets directory into a new file called assets.go as part of a package called webserver:

embedfiles -filename assets.go -package webserver -include-http -var Assets assets/*.html assets/*.css

Code within the assets package could then open index.html for read:

f, err := Assets.Open("assets/index.html")

or could use an instance as a file server (assuming the -include-http flag was set):

log.Fatal(http.ListenAndServe(":8080", http.FileServer(Assets))

As each call to embedfiles generates a completely self-contained .go file, multiple independent .go files can be generated and compiled into a single package by using different -varname options, allowing for discrete groups of files to be assigned to different variable names.

Usage:  embedfiles [arguments] <file glob> [<file glob> ...]

Arguments:

  -filename string
        File to write go output to.  Defaults to stdout (default "-")
  -include-http
        If true then the generated file will import net/http and comply with the http.FileSystem interface
  -package string
        Package name to use for output file. (default "main")
  -var string
        Variable name to assign the assets to.  Start with a capital letter to export from the package (default "assets")

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL