{"id":13413724,"url":"https://github.com/omeid/go-resources","last_synced_at":"2026-01-06T00:55:45.055Z","repository":{"id":27645190,"uuid":"31130055","full_name":"omeid/go-resources","owner":"omeid","description":"Unfancy resources embedding for Go with out of box http.FileSystem support.","archived":false,"fork":false,"pushed_at":"2022-10-19T08:15:30.000Z","size":1002,"stargazers_count":177,"open_issues_count":0,"forks_count":16,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-03-10T22:13:49.651Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/omeid.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-02-21T15:40:17.000Z","updated_at":"2025-01-08T09:26:56.000Z","dependencies_parsed_at":"2022-08-21T12:00:30.033Z","dependency_job_id":null,"html_url":"https://github.com/omeid/go-resources","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/omeid%2Fgo-resources","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/omeid%2Fgo-resources/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/omeid%2Fgo-resources/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/omeid%2Fgo-resources/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/omeid","download_url":"https://codeload.github.com/omeid/go-resources/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243635733,"owners_count":20322986,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-30T20:01:47.473Z","updated_at":"2026-01-06T00:55:45.048Z","avatar_url":"https://github.com/omeid.png","language":"Go","funding_links":[],"categories":["Resource Embedding","Go","资源嵌入","嵌入的资源","\u003cspan id=\"资源嵌入-resource-embedding\"\u003e资源嵌入 Resource Embedding\u003c/span\u003e","Relational Databases"],"sub_categories":["HTTP Clients","交流","HTTP客户端","Advanced Console UIs","查询语","\u003cspan id=\"高级控制台用户界面-advanced-console-uis\"\u003e高级控制台用户界面 Advanced Console UIs\u003c/span\u003e"],"readme":"\u003e Heads up!  \n\u003e This library is in maintenance mode. Please use the official embed library going forward.  \n\u003e https://pkg.go.dev/embed\n\n\n\n# Resources [![GoDoc](https://img.shields.io/badge/godoc-reference-blue.svg?style=flat-square)](https://godoc.org/github.com/omeid/go-resources)  [![Build Status](https://travis-ci.org/omeid/go-resources.svg?branch=master)](https://travis-ci.org/omeid/go-resources) [![Go Report Card](https://goreportcard.com/badge/github.com/omeid/go-resources?bust=true)](https://goreportcard.com/report/github.com/omeid/go-resources)\nUnfancy resources embedding with Go.\n\n- No blings.\n- No runtime dependency.\n- Idiomatic Library First design.\n\n### Dude, Why?\n\nYes, there is quite a lot of projects that handles resource embedding\nbut they come with more bling than you ever need and you often end up\nwith having dependencies for your end project. Not this time.\n\n### Installing\n\nJust go get it!\n\n```sh\n$ go get github.com/omeid/go-resources/cmd/resources\n```\n\n### Usage\n\n```\n$ resources -h\nUsage resources:\n  -declare\n        whether to declare the -var (default false)\n  -fmt\n        run output through gofmt, this is slow for huge files (default false)\n  -output filename\n        filename to write the output to\n  -package name\n        name of the package to generate (default \"main\")\n  -tag tag\n        tag to use for the generated package (default no tag)\n  -trim prefix\n        path prefix to remove from the resulting file path in the virtual filesystem\n  -var name\n        name of the variable to assign the virtual filesystem to (default \"FS\")\n  -width number\n        number of content bytes per line in generetated file (default 12)\n```\n\n### Optimization\n\nGenerating resources result in a very high number of lines of code, 1MB\nof resources result about 5MB of code at over 87,000 lines of code. This\nis caused by the chosen representation of the file contents within the\ngenerated file.\n\nInstead of a (binary) string, `resources` transforms each file into an\nactual byte slice. For example, a file with content `Hello, world!` will\nbe represented as follows:\n\n``` go\nFS = \u0026FileSystem{\n  \"/hello.txt\": File{\n    data: []byte{\n      0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64,\n      0x21,\n    },\n    fi: FileInfo{\n      name:    \"hello.txt\",\n      size:    13,\n      modTime: time.Unix(0, 1504640959536230658),\n      isDir:   false,\n    },\n  },\n}\n```\n\nWhile this seems wasteful, the compiled binary is not really affected.\n_If you add 1MB of resources, your binary will increase 1MB as well_.\n\nHowever, compiling this many lines of code takes time and slows down the\ncompiler. To avoid recompiling the resources every time and leverage the\ncompiler cache, generate your resources into a standalone package and\nthen import it, this will allow for faster iteration as you don't have\nto wait for the resources to be compiled with every change.\n\n``` sh\nmkdir -p assets\nresources -declare -var=FS -package=assets -output=assets/assets.go your/files/here\n```\n\n``` go\npackage main\n\nimport \"importpath/to/assets\"\n\nfunc main() {\n  data, err := assets.FS.Open(\"your/files/here\")\n  // ...\n}\n```\n\n##### \"Live\" development of resources\n\nFor fast iteration and improvement of your resources, you can work\naround the compile with the following technique:\n\nFirst, create a normal `main.go`:\n\n```go\npackage main\n\nimport \"net/http\"\n\nvar Assets http.FileSystem\n\nfunc main() {\n  if Assets == nil {\n    panic(\"No Assets. Have you generated the resources?\")\n  }\n\n  // use Assets here\n}\n```\n\nThen, add a second file in the same package (`main` here), with the\nfollowing content:\n\n```go\n// +build !embed\n\npackage main\n\nimport (\n\t\"net/http\"\n\n\t\"github.com/omeid/go-resources/live\"\n)\n\nvar Assets = live.Dir(\"./public\")\n```\n\nNow when you build or run your project, you will have files directly\nserved from `./public` directory.\n\nTo create a *production build*, i.e. one with the embedded files, build\nthe resouces with `-tag=embed` and add the `embed` tag to `go build`:\n\n```sh\n$ resources -output=public_resources.go -var=Assets -tag=embed public/*\n$ go build -tags=embed\n```\n\nNow your resources should be embedded with your program!\nOf course, you may use any `var` or `tag` name you please.\n\n### Go Generate\n\nThere is a few reasons to avoid resource embedding in `go generate`.\n\nFirst `go generate` is for generating Go source code from your code,\ngenerally the resources you want to embed aren't effected by the Go\nsource directly and as such generating resources are slightly out of the\nscope of `go generate`.\n\nSecond, you're unnecessarily slowing down code iterations by blocking\n`go generate` for resource generation.\n\n# Resources, The Library [![GoDoc](https://godoc.org/github.com/omeid/go-resources?status.svg)](https://godoc.org/github.com/omeid/go-resources)\n\nThe resource generator is written as a library and isn't bound to\nfilesystem by the way of accepting files in the form\n\n```go\ntype File interface {\n      io.Reader\n      Stat() (os.FileInfo, error)\n}\n```\n\nalong with a helper method that adds files from filesystem.\n\nThis allows to integrate `resources` with ease in your workflow when the\nwhen the provided command doesn't fit well, for an example see the [Gonzo\nbinding](https://github.com/go-gonzo/resources/blob/master/resources.go)\n`resources`.\n\nPlease refer to the [GoDoc](https://godoc.org/github.com/omeid/go-resources)\nfor complete documentation.\n\n### Strings\n\nThe generated FileSystem also implements an `String(string) (string, bool)` method that allows you to read the content of a file as string, to use that\ninstead of defining your file Assets variable as simply an http.FileSystem, do the following:\n\n```go\ntype Resources interface {\n\thttp.FileSystem\n\tString(string) (string, bool)\n}\n\nvar Assets Resources\n```\n\nNow you can call `Assets.String(someFile)` and get the content as string with a boolean value indicating whatever the file was found or not.\n\n---\n\n### Contributing\n\nPlease consider opening an issue first, or just send a pull request. :)\n\n### Credits\n\nSee [Contributors](https://github.com/omeid/go-resources/graphs/contributors).\n\n### LICENSE\n\n[MIT](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fomeid%2Fgo-resources","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fomeid%2Fgo-resources","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fomeid%2Fgo-resources/lists"}