{"id":15747769,"url":"https://github.com/developer-guy/static-file-embedding-in-go-using-pkger","last_synced_at":"2025-03-31T06:41:45.516Z","repository":{"id":104883626,"uuid":"337773485","full_name":"developer-guy/static-file-embedding-in-go-using-pkger","owner":"developer-guy","description":"Demonstrating how you can embed static files to inside of your go binary","archived":false,"fork":false,"pushed_at":"2021-02-10T16:10:36.000Z","size":3,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-06T11:23:12.859Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/developer-guy.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-02-10T15:53:43.000Z","updated_at":"2021-02-25T19:25:00.000Z","dependencies_parsed_at":null,"dependency_job_id":"aea99b63-ef55-42d3-a296-fbcfaf5c0d4d","html_url":"https://github.com/developer-guy/static-file-embedding-in-go-using-pkger","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/developer-guy%2Fstatic-file-embedding-in-go-using-pkger","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developer-guy%2Fstatic-file-embedding-in-go-using-pkger/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developer-guy%2Fstatic-file-embedding-in-go-using-pkger/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developer-guy%2Fstatic-file-embedding-in-go-using-pkger/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/developer-guy","download_url":"https://codeload.github.com/developer-guy/static-file-embedding-in-go-using-pkger/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246429449,"owners_count":20775805,"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-10-04T05:21:45.742Z","updated_at":"2025-03-31T06:41:45.499Z","avatar_url":"https://github.com/developer-guy.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Description\n\nOne of the best parts of Go is that compiled programs can be distributed and executed as a single dependency-free binary file. Unfortunately, programs requiring access to static files for thing like configuration, web server assets, or database migrations must be distributed alongside those static files, eliminating the single-file benefit. Fortunately, there's a way to bundle these assets inside the binary itself.\n\nThis post demonstrates how to embed static assets inside Go binaries using the pkger.\n\n\u003e Credit: https://codesalad.dev/blog/embed-static-files-in-go-binaries-with-pkger-15\n\n# What is pkger?\npkger is a tool for embedding static files into Go binaries.\n\n# Hands On\nFirst thing that we need to do is generating pkged.go source file. In order to do that we need a \"pkger\" cli, so the following command will install this utility to our host\n```bash\n$ go get github.com/markbates/pkger/cmd/pkger\n```\n\nThen clone this repository and create pkged.go source file using the following command:\n```bash\n$ pkger\n```\n\nTo check what's happening, we can also run pkger list to preview the assets that will be embedded.\n```bash\n$ pkger list\ngithub.com/developer-guy/static-file-embedding-in-go-using-pkger\n \u003e github.com/developer-guy/static-file-embedding-in-go-using-pkger:/hello-world.txt\n```\n\nLets dockerize our application and build based on empty layer using special image called \"scratch\".\n```bash\n$ docker image build -t static-embed:v1 .\n[+] Building 4.4s (14/14) FINISHED\n =\u003e [internal] load build definition from Dockerfile                                                                                                                                                        0.0s\n =\u003e =\u003e transferring dockerfile: 320B                                                                                                                                                                        0.0s\n =\u003e [internal] load .dockerignore                                                                                                                                                                           0.0s\n =\u003e =\u003e transferring context: 2B                                                                                                                                                                             0.0s\n =\u003e [internal] load metadata for docker.io/library/golang:1.15.7-alpine                                                                                                                                     1.8s\n =\u003e [auth] library/golang:pull token for registry-1.docker.io                                                                                                                                               0.0s\n =\u003e CACHED [stage-1 1/2] WORKDIR /app                                                                                                                                                                       0.0s\n =\u003e [stage-0 1/6] FROM docker.io/library/golang:1.15.7-alpine@sha256:dbda4e47937a3abb515c386d955002be5116d060c90d936127cc24ac439c815c                                                                       0.0s\n =\u003e [internal] load build context                                                                                                                                                                           0.0s\n =\u003e =\u003e transferring context: 29.98kB                                                                                                                                                                        0.0s\n =\u003e CACHED [stage-0 2/6] WORKDIR /app                                                                                                                                                                       0.0s\n =\u003e CACHED [stage-0 3/6] COPY go.mod go.sum ./                                                                                                                                                              0.0s\n =\u003e CACHED [stage-0 4/6] RUN go mod download                                                                                                                                                                0.0s\n =\u003e [stage-0 5/6] COPY ./ ./                                                                                                                                                                                0.0s\n =\u003e [stage-0 6/6] RUN go build -o static-embed                                                                                                                                                              2.4s\n =\u003e [stage-1 2/2] COPY --from=0 /app/static-embed ./                                                                                                                                                        0.0s\n =\u003e exporting to image                                                                                                                                                                                      0.0s\n =\u003e =\u003e exporting layers                                                                                                                                                                                     0.0s\n =\u003e =\u003e writing image sha256:013112fa5145b4f6e7cdebe7e5dd292d6ac81ff868ea58f84e36a346c23c66bc                                                                                                                0.0s\n =\u003e =\u003e naming to docker.io/library/static-embed:v1                                                                                                                                                          0.0s\n```\n\nLets run and verify the output that should be match with the content of the file\n```bash\n$ docker container run static-embed:v1\nName:  hello-world.txt\nSize:  12\nMode:  -rw-r--r--\nModTime:  2021-02-10 18:37:49.168973512 +0300 +0300\nhello world\n```\n\n# References\n\n* https://codesalad.dev/blog/embed-static-files-in-go-binaries-with-pkger-15\n* https://github.com/markbates/pkger\n* https://blog.gobuffalo.io/introducing-pkger-static-file-embedding-in-go-1ce76dc79c65\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdeveloper-guy%2Fstatic-file-embedding-in-go-using-pkger","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdeveloper-guy%2Fstatic-file-embedding-in-go-using-pkger","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdeveloper-guy%2Fstatic-file-embedding-in-go-using-pkger/lists"}