Fix up embed-directive example and rename files to be shorter

This commit is contained in:
Eli Bendersky
2022-05-24 06:11:22 -07:00
parent d4bbdc258b
commit fc88db1814
7 changed files with 70 additions and 86 deletions

View File

@@ -1,45 +1,42 @@
// `//go:embed` is a compiler directive that allows programs to include arbitrary
// files/folders in the binary. Read more about go directive
// [here](https://dave.cheney.net/2018/01/08/gos-hidden-pragmas).
// `//go:embed` is a compiler directive that allows programs to include
// arbitrary files/folders in the binary at build time. Read more about go
// directives [here](https://pkg.go.dev/cmd/compile#hdr-Compiler_Directives)
// and about the embed directive [here](https://pkg.go.dev/embed).
package main
// If no exported identifiers is directly used from `embed` package,
// you can add an underscore `_` before the package name. In this example, we simply
// import it as we need to use the `embed.FS` type from the package.
// Import the `embed` package; if you don't use any exported
// identifiers from this package, you can do a blank import with `_ "embed"`.
import (
//_ "embed"
"embed"
)
// Since one file is defined after the directive, the compiler will only include
// this single file, followed by variable `single_file_string` to access as `string` type.
//go:embed example_folder/single_file.txt
var single_file_string string
// embed directives accept paths relative to the directory containing the
// Go source file. This directive embeds the contents of the file into a
// `string` variable.
//go:embed folder/single_file.txt
var fileString string
// Here is a similar example but include single file as `[]byte`.
//go:embed example_folder/single_file.txt
var single_file_byte []byte
// Or embed the contents of the file into a `[]byte`.
//go:embed folder/single_file.txt
var fileByte []byte
// We can also embed multiple files or even folders with wildcard.
//go:embed example_folder/single_file.txt
//go:embed example_folder/*.hash
var folder_FS embed.FS
// We can also embed multiple files or even folders with wildcards. This uses
// a variable of the [embed.FS type](https://pkg.go.dev/embed#FS), which
// implements a simple virtual file system.
//go:embed folder/single_file.txt
//go:embed folder/*.hash
var folder embed.FS
func main() {
// Print out content of `example_single_file.txt` as `string`.
print(single_file_string)
// Print out the contents of `single_file.txt`.
print(fileString)
print(string(fileByte))
// Now handle `[]byte`.
print(string(single_file_byte))
// Retrieve file(s) matching `*.hash` pattern by reading from variable `folder_FS` first,
// then print out.
hash_file1 := "example_folder/multi_file1.hash"
hash_file2 := "example_folder/multi_file2.hash"
hash_content1, _ := folder_FS.ReadFile(hash_file1)
hash_content2, _ := folder_FS.ReadFile(hash_file2)
print(string(hash_content1))
print(string(hash_content2))
// Retrieve some files from the embedded folder.
content1, _ := folder.ReadFile("folder/file1.hash")
print(string(content1))
content2, _ := folder.ReadFile("folder/file2.hash")
print(string(content2))
}

View File

@@ -1,2 +1,2 @@
e996755d889f01c99c353616fb2bdeac6c3be6e6
-EXJc75EbN-
82ad92029645b4fd1b5ef0cd9df3fecdf0a6764e
myu7kywm7oI

View File

@@ -1,10 +1,10 @@
# Use these commands to run the example.
# (Note: due to limitation on go playground,
# this example can only be run on your local machine.)
$ mkdir -p example_folder
$ echo "hello go" > example_folder/single_file.txt
$ echo "123" > example_folder/multi_file1.hash
$ echo "456" > example_folder/multi_file2.hash
$ mkdir -p folder
$ echo "hello go" > folder/single_file.txt
$ echo "123" > folder/file1.hash
$ echo "456" > folder/file2.hash
$ go run embed-directive.go
hello go