lresource

package module
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2019 License: MIT Imports: 11 Imported by: 5

README

lresource

lresource is a simple solution for embedding static resources inside of Go binaries.

Install

go get github.com/belfinor/lresource

Embed resource

You have directory data, that contains text.txt file (may be binary) of the following contents:

English texts for beginners to practice reading and comprehension online and for free. Practicing your comprehension of written English will both improve your vocabulary and understanding of grammar and word order. The texts below are designed to help you develop while giving you an instant evaluation of your progress.

Prepared by experienced English teachers, the texts, articles and conversations are brief and appropriate to your level of proficiency. Take the multiple-choice quiz following each text, and you'll get the results immediately. You will feel both challenged and accomplished! You can even download (as PDF) and print the texts and exercises. It's enjoyable, fun and free. Good luck!

Create generator.go into data:

// +build ignore

package main

import (

	"github.com/belfinor/lresource"
)

func main() {

	err := lresource.Convert("text.txt", "text.txt.go", "data", "data/text.txt", false)
	if err != nil {
		panic(err)
	}
}

Create data.go with generate instruction:

package data

//go:generate go run generator.go

Go to data and run generate:

cd data
go generate

And you have file text.txt.go like this:

package data

// @comment this file was generated by lresource

import (
  "github.com/belfinor/lresource"
)

func init() {

  lresource.Add("data/text.txt", "text/plain; charset=utf-8", true, `
H4sIAAAAAAAA/1ySPY7cMAyF+z0Ft9oEmPgU+UG6KbZJKUvPNrM0qVCyPc7pA2lm
sUBa0w/fRz5901m4LFRxq4Umcxoxsyq8UDXKHmLlCHKExDpT0ETR1uxYoIVNyVRY
0QctPjkw0PWea4nTNv8/MtHhXCuU3vkHi9BodSFes9uOe263GMZNgp8dsGmClxq0
u9hEs4d1Dd6Hh3ki8wQf6HXBY6URYgcFByUUnhWp7bVAciNQwg6xTMfCApp5fyhT
UGJtqErYg2yhPsy7V3abHaUMT09XRw6ORONJuGU4QyMSfVw2xAVeLlTfpS4UvHIU
lMc9dYeXTihddXTG1GchZ7fsHCqad4dLc24q2W3i2HjnQK/hDR2xblI5C77ExVp1
fzb+S5OJ2NGWazpd49IBp20vIjSj9rCjbFIL8boiNaqcA/2y7V7QBDxaiksQgc5I
d83YGm77Ij33/2NQwg6lZIeKhUSfQqHr1++feyA7a/24SP+GGzxyQRnoZ30pBP1t
ZxgFF5o2vb+w/rp+mCWSLb49P/0LAAD///xIsyHAAgAA`)

}

Access resource from code

import (

	"github.com/belfinor/lresource"
	_ "github.com/login/proj/data" // load data

)

func Get() []byte {

  data := lresource.Get("data/text.txt")

	if data != nil {
		return data.Data
	}

  return nil
}

Delete resource by name

lresource.Delete("data/text.txt")

If resource changed

If the data.txt file has changed, you go to the data directory and call go generate.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add

func Add(name, contentType string, arch bool, content string)

func Convert added in v1.0.4

func Convert(src string, dest string, pkg string, name string, noarch bool) error

func Delete added in v1.0.5

func Delete(name string)

Types

type FileData added in v1.0.1

type FileData struct {
	Name        string
	ContentType string
	Data        []byte
}

func Get

func Get(name string) *FileData

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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