1 // Copyright 2020 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Package embed provides access to files embedded in the running Go program.
7 // Go source files that import "embed" can use the //go:embed directive
8 // to initialize a variable of type string, []byte, or [FS] with the contents of
9 // files read from the package directory or subdirectories at compile time.
11 // For example, here are three ways to embed a file named hello.txt
12 // and then print its contents at run time.
14 // Embedding one file into a string:
18 // //go:embed hello.txt
22 // Embedding one file into a slice of bytes:
26 // //go:embed hello.txt
30 // Embedded one or more files into a file system:
34 // //go:embed hello.txt
36 // data, _ := f.ReadFile("hello.txt")
37 // print(string(data))
41 // A //go:embed directive above a variable declaration specifies which files to embed,
42 // using one or more path.Match patterns.
44 // The directive must immediately precede a line containing the declaration of a single variable.
45 // Only blank lines and ‘//’ line comments are permitted between the directive and the declaration.
47 // The type of the variable must be a string type, or a slice of a byte type,
48 // or [FS] (or an alias of [FS]).
56 // // content holds our static web server content.
57 // //go:embed image/* template/*
58 // //go:embed html/index.html
59 // var content embed.FS
61 // The Go build system will recognize the directives and arrange for the declared variable
62 // (in the example above, content) to be populated with the matching files from the file system.
64 // The //go:embed directive accepts multiple space-separated patterns for
65 // brevity, but it can also be repeated, to avoid very long lines when there are
66 // many patterns. The patterns are interpreted relative to the package directory
67 // containing the source file. The path separator is a forward slash, even on
68 // Windows systems. Patterns may not contain ‘.’ or ‘..’ or empty path elements,
69 // nor may they begin or end with a slash. To match everything in the current
70 // directory, use ‘*’ instead of ‘.’. To allow for naming files with spaces in
71 // their names, patterns can be written as Go double-quoted or back-quoted
74 // If a pattern names a directory, all files in the subtree rooted at that directory are
75 // embedded (recursively), except that files with names beginning with ‘.’ or ‘_’
76 // are excluded. So the variable in the above example is almost equivalent to:
78 // // content is our static web server content.
79 // //go:embed image template html/index.html
80 // var content embed.FS
82 // The difference is that ‘image/*’ embeds ‘image/.tempfile’ while ‘image’ does not.
83 // Neither embeds ‘image/dir/.tempfile’.
85 // If a pattern begins with the prefix ‘all:’, then the rule for walking directories is changed
86 // to include those files beginning with ‘.’ or ‘_’. For example, ‘all:image’ embeds
87 // both ‘image/.tempfile’ and ‘image/dir/.tempfile’.
89 // The //go:embed directive can be used with both exported and unexported variables,
90 // depending on whether the package wants to make the data available to other packages.
91 // It can only be used with variables at package scope, not with local variables.
93 // Patterns must not match files outside the package's module, such as ‘.git/*’ or symbolic links.
94 // Patterns must not match files whose names include the special punctuation characters " * < > ? ` ' | / \ and :.
95 // Matches for empty directories are ignored. After that, each pattern in a //go:embed line
96 // must match at least one file or non-empty directory.
98 // If any patterns are invalid or have invalid matches, the build will fail.
100 // # Strings and Bytes
102 // The //go:embed line for a variable of type string or []byte can have only a single pattern,
103 // and that pattern can match only a single file. The string or []byte is initialized with
104 // the contents of that file.
106 // The //go:embed directive requires importing "embed", even when using a string or []byte.
107 // In source files that don't refer to [embed.FS], use a blank import (import _ "embed").
111 // For embedding a single file, a variable of type string or []byte is often best.
112 // The [FS] type enables embedding a tree of files, such as a directory of static
113 // web server content, as in the example above.
115 // FS implements the [io/fs] package's [FS] interface, so it can be used with any package that
116 // understands file systems, including [net/http], [text/template], and [html/template].
118 // For example, given the content variable in the example above, we can write:
120 // http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(content))))
122 // template.ParseFS(content, "*.tmpl")
126 // To support tools that analyze Go packages, the patterns found in //go:embed lines
127 // are available in “go list” output. See the EmbedPatterns, TestEmbedPatterns,
128 // and XTestEmbedPatterns fields in the “go help list” output.
138 // An FS is a read-only collection of files, usually initialized with a //go:embed directive.
139 // When declared without a //go:embed directive, an FS is an empty file system.
141 // An FS is a read-only value, so it is safe to use from multiple goroutines
142 // simultaneously and also safe to assign values of type FS to each other.
144 // FS implements fs.FS, so it can be used with any package that understands
145 // file system interfaces, including net/http, text/template, and html/template.
147 // See the package documentation for more details about initializing an FS.
149 // The compiler knows the layout of this struct.
150 // See cmd/compile/internal/staticdata's WriteEmbed.
152 // The files list is sorted by name but not by simple string comparison.
153 // Instead, each file's name takes the form "dir/elem" or "dir/elem/".
154 // The optional trailing slash indicates that the file is itself a directory.
155 // The files list is sorted first by dir (if dir is missing, it is taken to be ".")
156 // and then by base, so this list of files:
167 // is actually sorted as:
172 // q/r # dir=q elem=r
173 // q/s/ # dir=q elem=s
174 // q/v # dir=q elem=v
175 // q/s/t # dir=q/s elem=t
176 // q/s/u # dir=q/s elem=u
178 // This order brings directory contents together in contiguous sections
179 // of the list, allowing a directory read to use binary search to find
180 // the relevant sequence of entries.
184 // split splits the name into dir and elem as described in the
185 // comment in the FS struct above. isDir reports whether the
186 // final trailing slash was present, indicating that name is a directory.
187 func split(name string) (dir, elem string, isDir bool) {
188 if name[len(name)-1] == '/' {
190 name = name[:len(name)-1]
193 for i >= 0 && name[i] != '/' {
197 return ".", name, isDir
199 return name[:i], name[i+1:], isDir
202 // trimSlash trims a trailing slash from name, if present,
203 // returning the possibly shortened name.
204 func trimSlash(name string) string {
205 if len(name) > 0 && name[len(name)-1] == '/' {
206 return name[:len(name)-1]
212 _ fs.ReadDirFS = FS{}
213 _ fs.ReadFileFS = FS{}
216 // A file is a single file in the FS.
217 // It implements fs.FileInfo and fs.DirEntry.
219 // The compiler knows the layout of this struct.
220 // See cmd/compile/internal/staticdata's WriteEmbed.
223 hash [16]byte // truncated SHA256 hash
227 _ fs.FileInfo = (*file)(nil)
228 _ fs.DirEntry = (*file)(nil)
231 func (f *file) Name() string { _, elem, _ := split(f.name); return elem }
232 func (f *file) Size() int64 { return int64(len(f.data)) }
233 func (f *file) ModTime() time.Time { return time.Time{} }
234 func (f *file) IsDir() bool { _, _, isDir := split(f.name); return isDir }
235 func (f *file) Sys() any { return nil }
236 func (f *file) Type() fs.FileMode { return f.Mode().Type() }
237 func (f *file) Info() (fs.FileInfo, error) { return f, nil }
239 func (f *file) Mode() fs.FileMode {
241 return fs.ModeDir | 0555
246 func (f *file) String() string {
247 return fs.FormatFileInfo(f)
250 // dotFile is a file for the root directory,
251 // which is omitted from the files list in a FS.
252 var dotFile = &file{name: "./"}
254 // lookup returns the named file, or nil if it is not present.
255 func (f FS) lookup(name string) *file {
256 if !fs.ValidPath(name) {
257 // The compiler should never emit a file with an invalid name,
258 // so this check is not strictly necessary (if name is invalid,
259 // we shouldn't find a match below), but it's a good backstop anyway.
269 // Binary search to find where name would be in the list,
270 // and then check if name is at that position.
271 dir, elem, _ := split(name)
273 i := sortSearch(len(files), func(i int) bool {
274 idir, ielem, _ := split(files[i].name)
275 return idir > dir || idir == dir && ielem >= elem
277 if i < len(files) && trimSlash(files[i].name) == name {
283 // readDir returns the list of files corresponding to the directory dir.
284 func (f FS) readDir(dir string) []file {
288 // Binary search to find where dir starts and ends in the list
289 // and then return that slice of the list.
291 i := sortSearch(len(files), func(i int) bool {
292 idir, _, _ := split(files[i].name)
295 j := sortSearch(len(files), func(j int) bool {
296 jdir, _, _ := split(files[j].name)
302 // Open opens the named file for reading and returns it as an [fs.File].
304 // The returned file implements [io.Seeker] and [io.ReaderAt] when the file is not a directory.
305 func (f FS) Open(name string) (fs.File, error) {
306 file := f.lookup(name)
308 return nil, &fs.PathError{Op: "open", Path: name, Err: fs.ErrNotExist}
311 return &openDir{file, f.readDir(name), 0}, nil
313 return &openFile{file, 0}, nil
316 // ReadDir reads and returns the entire named directory.
317 func (f FS) ReadDir(name string) ([]fs.DirEntry, error) {
318 file, err := f.Open(name)
322 dir, ok := file.(*openDir)
324 return nil, &fs.PathError{Op: "read", Path: name, Err: errors.New("not a directory")}
326 list := make([]fs.DirEntry, len(dir.files))
327 for i := range list {
328 list[i] = &dir.files[i]
333 // ReadFile reads and returns the content of the named file.
334 func (f FS) ReadFile(name string) ([]byte, error) {
335 file, err := f.Open(name)
339 ofile, ok := file.(*openFile)
341 return nil, &fs.PathError{Op: "read", Path: name, Err: errors.New("is a directory")}
343 return []byte(ofile.f.data), nil
346 // An openFile is a regular file open for reading.
347 type openFile struct {
348 f *file // the file itself
349 offset int64 // current read offset
353 _ io.Seeker = (*openFile)(nil)
354 _ io.ReaderAt = (*openFile)(nil)
357 func (f *openFile) Close() error { return nil }
358 func (f *openFile) Stat() (fs.FileInfo, error) { return f.f, nil }
360 func (f *openFile) Read(b []byte) (int, error) {
361 if f.offset >= int64(len(f.f.data)) {
365 return 0, &fs.PathError{Op: "read", Path: f.f.name, Err: fs.ErrInvalid}
367 n := copy(b, f.f.data[f.offset:])
372 func (f *openFile) Seek(offset int64, whence int) (int64, error) {
379 offset += int64(len(f.f.data))
381 if offset < 0 || offset > int64(len(f.f.data)) {
382 return 0, &fs.PathError{Op: "seek", Path: f.f.name, Err: fs.ErrInvalid}
388 func (f *openFile) ReadAt(b []byte, offset int64) (int, error) {
389 if offset < 0 || offset > int64(len(f.f.data)) {
390 return 0, &fs.PathError{Op: "read", Path: f.f.name, Err: fs.ErrInvalid}
392 n := copy(b, f.f.data[offset:])
399 // An openDir is a directory open for reading.
400 type openDir struct {
401 f *file // the directory file itself
402 files []file // the directory contents
403 offset int // the read offset, an index into the files slice
406 func (d *openDir) Close() error { return nil }
407 func (d *openDir) Stat() (fs.FileInfo, error) { return d.f, nil }
409 func (d *openDir) Read([]byte) (int, error) {
410 return 0, &fs.PathError{Op: "read", Path: d.f.name, Err: errors.New("is a directory")}
413 func (d *openDir) ReadDir(count int) ([]fs.DirEntry, error) {
414 n := len(d.files) - d.offset
421 if count > 0 && n > count {
424 list := make([]fs.DirEntry, n)
425 for i := range list {
426 list[i] = &d.files[d.offset+i]
432 // sortSearch is like sort.Search, avoiding an import.
433 func sortSearch(n int, f func(int) bool) int {
434 // Define f(-1) == false and f(n) == true.
435 // Invariant: f(i-1) == false, f(j) == true.
438 h := int(uint(i+j) >> 1) // avoid overflow when computing h
441 i = h + 1 // preserves f(i-1) == false
443 j = h // preserves f(j) == true
446 // i == j, f(i-1) == false, and f(j) (= f(i)) == true => answer is i.