Rewrites string or []byte variables with the content of files.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
scripts
testdata
vendor/github.com/urfave/cli
LICENSE
README.md
glide.lock
glide.yaml
main.go
source.go
util.go

README.md

embed

Hacked together prototype to rewrite string or []byte variables and constants in a package, using the content of a file.

Meant to be used with go generate, comming in go 1.4.

Usage

$ cd testdata/
$ embed file -var createDbSQL --source create_query.sql

or

package testdata

//go:generate embed file -var createDbSQL --source create_query.sql
var createDbSQL string

Flags:

  • -dir: look for Go files in testdata, defaults to current directory
  • -var: set the variable createDbSQL
  • --source or stdin: source of content for createDbSQL
  • --keep: creates a new file instead of setting the variable directly in the file

Installation

linux

wget -qO- https://github.com/aybabtme/embed/releases/download/0.1/embed_linux.tar.gz | tar xvz

darwin

wget -qO- https://github.com/aybabtme/embed/releases/download/0.1/embed_darwin.tar.gz | tar xvz

Example

With go generate

If you have a Go file:

package testdata

//go:generate embed file -var createDbSQL --source create_query.sql
var createDbSQL string

And invoke:

$ cd testdata/
$ go generate
embed: in file "example.go"; value of "createDbSQL" set

Now the file contains:

package testdata

//go:generate embed file -var createDbSQL --source create_query.sql
var createDbSQL = "CREATE DATABASE IF NOT EXISTS hello_world;\n"

Manually

With the following files:

  • testdata/create_query.sql
CREATE DATABASE IF NOT EXISTS hello_world;
  • testdata/example.go
package testdata

import (
    "database/sql"
    _ "github.com/go-sql-driver/mysql"
)

var createDbSQL string

func CreateDB(dsn string) error {
    db, err := sql.Open("mysql", dsn)
    if err != nil {
        return err
    }
    _, err := db.Exec(createDbSQL)
    return err
}

Running this command:

$ embed file --keep -dir testdata -var createDbSQL < testdata/create_query.sql

Gives you:

  • testdata/generated_example.go
package testdata

import (
    "database/sql"
    _ "github.com/go-sql-driver/mysql"
)

var createDbSQL = "CREATE DATABASE IF NOT EXISTS hello_world;\n"

func CreateDB(dsn string) error {
    db, err := sql.Open("mysql", dsn)
    if err != nil {
        return err
    }
    _, err := db.Exec(createDbSQL)
    return err
}