diff --git a/examples/embed-directive/embed-directive.go b/examples/embed-directive/embed-directive.go index 2424519..d7b1c13 100644 --- a/examples/embed-directive/embed-directive.go +++ b/examples/embed-directive/embed-directive.go @@ -27,13 +27,13 @@ var folder_FS embed.FS func main() { - // Print out the content of `example_single_file.txt` as `string`. + // Print out content of `example_single_file.txt` as `string`. print(single_file_string) - // Now handle `[]byte`.` + // Now handle `[]byte`. print(string(single_file_byte)) - // Retrieve the file(s) matching `*.hash` pattern by reading from `folder_FS` first, + // 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" diff --git a/examples/embed-directive/embed-directive.hash b/examples/embed-directive/embed-directive.hash index f749cc5..bf62e8b 100644 --- a/examples/embed-directive/embed-directive.hash +++ b/examples/embed-directive/embed-directive.hash @@ -1,2 +1,2 @@ -33356517e565ff2d1f9d7f935012a326e8f5b3ea -yTsQsPe9jth +e996755d889f01c99c353616fb2bdeac6c3be6e6 +-EXJc75EbN- diff --git a/examples/embed-directive/embed-directive.sh b/examples/embed-directive/embed-directive.sh index 5174a03..f6937db 100644 --- a/examples/embed-directive/embed-directive.sh +++ b/examples/embed-directive/embed-directive.sh @@ -1,7 +1,6 @@ -# Use these commands to run the example -# Note: due to limitation on go playground -# This example can only be run on your local machine - +# 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 diff --git a/public/embed-directive b/public/embed-directive index 32703e9..35536bb 100644 --- a/public/embed-directive +++ b/public/embed-directive @@ -9,12 +9,12 @@ onkeydown = (e) => { if (e.key == "ArrowLeft") { - window.location.href = 'struct-embedding'; + window.location.href = 'temporary-files-and-directories'; } if (e.key == "ArrowRight") { - window.location.href = 'generics'; + window.location.href = 'testing-and-benchmarking'; } } @@ -27,26 +27,13 @@
Starting in Go version 1.16+, Go supports use of //go:embed
-which is a directive to embed files and folders into
-the application binary by the compiler.
Since Go does not have preprocessor or macros like C type of languages,
-pragmas/directives
are implemented by the compiler as comments //
.
-Using //go:embed
tells the compiler to include arbitrary files/folders in binary.
//go:embed
is a compiler directive that allows programs to include arbitrary
+files/folders in the binary. Read more about go directive
+here.
package main@@ -55,17 +42,17 @@ Using
//go:embed
tells the compiler to include arbitrary files/fold
If you are not using any exported identifiers from embed
package directly,
-you can add an underscore _
before the package name, ask Go to import embed
-without directly using it, like this: _ "embed"
. In this example, we remove the
-underscore as we need to use the embed.FS
type from the package.
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 ( - "embed" + //_ "embed" + "embed" )
single_file_string
to access
-//go:embed embed-directive.sh
+//go:embed example_folder/single_file.txt
var single_file_string string
single_file_string
to access
Here is a similar example but include single file as []byte
.
-Note that //go:embed
can only be used in package level,
-define it in any function will result in error.
Here is a similar example but include single file as []byte
.
-//go:embed embed-directive.sh
+//go:embed example_folder/single_file.txt
var single_file_byte []byte
We can also embed multiple files or even folders with wildcard.
-Since Go resolves these files and folders during compile time, we have to use the files in
-current working directory as examples. In practice, you can embed files with "//go:embed /path/to/folder"
-or with another example "//go:embed /path/to/folder/*"
. You can add multiple //go:embed
before defining a variable.
We can also embed multiple files or even folders with wildcard.
-//go:embed *.hash -//go:embed embed-directive.sh +//go:embed example_folder/single_file.txt +//go:embed example_folder/*.hash var folder_FS embed.FS
"//go:embed /path/to/folder/*"
. Y
Print out the content of embed-directive.sh
as string
.
Print out content of example_single_file.txt
as string
.
- println(single_file_string) + print(single_file_string)
Print out the content of embed-directive.sh
as []byte
.
Now handle []byte
.
- println(string(single_file_byte)) + print(string(single_file_byte))
Print out the content of embed-directive.hash
by reading the file from folder_FS
first,
-then print out the content.
Retrieve file(s) matching *.hash
pattern by reading from variable folder_FS
first,
+then print out.
- hash_file := "embed-directive.hash" - hash_content, _ := folder_FS.ReadFile(hash_file) - println(string(hash_content)) + 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))
Use these commands to run the example
+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+
-$ go run embed-directive.go
+ $ go run embed-directive.go +hello go +hello go +123 +456
- Next example: Generics. + Next example: Testing and Benchmarking.
@@ -215,7 +219,7 @@ then print out the content.