diff --git a/examples/embed-directive/embed-directive.go b/examples/embed-directive/embed-directive.go index d7b1c13..bd3a976 100644 --- a/examples/embed-directive/embed-directive.go +++ b/examples/embed-directive/embed-directive.go @@ -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)) } diff --git a/examples/embed-directive/embed-directive.hash b/examples/embed-directive/embed-directive.hash index bf62e8b..170913f 100644 --- a/examples/embed-directive/embed-directive.hash +++ b/examples/embed-directive/embed-directive.hash @@ -1,2 +1,2 @@ -e996755d889f01c99c353616fb2bdeac6c3be6e6 --EXJc75EbN- +82ad92029645b4fd1b5ef0cd9df3fecdf0a6764e +myu7kywm7oI diff --git a/examples/embed-directive/embed-directive.sh b/examples/embed-directive/embed-directive.sh index f6937db..cae33d8 100644 --- a/examples/embed-directive/embed-directive.sh +++ b/examples/embed-directive/embed-directive.sh @@ -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 diff --git a/examples/embed-directive/example_folder/multi_file1.hash b/examples/embed-directive/folder/file1.hash similarity index 100% rename from examples/embed-directive/example_folder/multi_file1.hash rename to examples/embed-directive/folder/file1.hash diff --git a/examples/embed-directive/example_folder/multi_file2.hash b/examples/embed-directive/folder/file2.hash similarity index 100% rename from examples/embed-directive/example_folder/multi_file2.hash rename to examples/embed-directive/folder/file2.hash diff --git a/examples/embed-directive/example_folder/single_file.txt b/examples/embed-directive/folder/single_file.txt similarity index 100% rename from examples/embed-directive/example_folder/single_file.txt rename to examples/embed-directive/folder/single_file.txt diff --git a/public/embed-directive b/public/embed-directive index 35536bb..2743d7d 100644 --- a/public/embed-directive +++ b/public/embed-directive @@ -27,13 +27,14 @@ -

//go:embed is a compiler directive that allows programs to include arbitrary -files/folders in the binary. Read more about go directive -here.

+

//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 +and about the embed directive here.

- +
 package main
 
@@ -42,17 +43,15 @@ files/folders in the binary. Read more about go directive -

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"
+    "embed"
 )
 
@@ -60,44 +59,47 @@ import it as we need to use the embed.FS type from the package.

-

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.

+

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 example_folder/single_file.txt
-var single_file_string string
+//go:embed folder/single_file.txt
+var fileString string
 
-

Here is a similar example but include single file as []byte.

+

Or embed the contents of the file into a []byte.

-//go:embed example_folder/single_file.txt
-var single_file_byte []byte
+//go:embed folder/single_file.txt
+var fileByte []byte
 
-

We can also embed multiple files or even folders with wildcard.

+

We can also embed multiple files or even folders with wildcards. This uses +a variable of the embed.FS type, which +implements a simple virtual file system.

-//go:embed example_folder/single_file.txt
-//go:embed example_folder/*.hash
-var folder_FS embed.FS
+//go:embed folder/single_file.txt
+//go:embed folder/*.hash
+var folder embed.FS
 
@@ -115,45 +117,28 @@ this single file, followed by variable single_file_string to access -

Print out content of example_single_file.txt as string.

+

Print out contents of single_file.txt.

-    print(single_file_string)
+    print(fileString)
+    print(string(fileByte))
 
-

Now handle []byte.

+

Retrieve some files from the embedded folder.

-    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))
+    content1, _ := folder.ReadFile("folder/file1.hash")
+    print(string(content1))
 
@@ -164,7 +149,9 @@ then print out.

-
}
+          
    content2, _ := folder.ReadFile("folder/file2.hash")
+    print(string(content2))
+}
 
@@ -183,10 +170,10 @@ 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
@@ -219,7 +206,7 @@ this example can only be run on your local machine.)