embed

command module
v0.0.0-...-79dee93 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2016 License: MIT Imports: 13 Imported by: 0

README

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
}

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

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