From 1d932995f6fc94deb26e8a7281c92a315c5fd74d Mon Sep 17 00:00:00 2001
From: peterzhu1992 Starting in Go version 1.16+, Go supports use of Since Go does not have preprocessor or macros like C type of languages,
- If you are not using any exported identifiers from If no exported identifiers is directly used from Here is a similar example but include single file as Here is a similar example but include single file as 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 We can also embed multiple files or even folders with wildcard. Print out the content of Print out content of Print out the content of Now handle Print out the content of Retrieve file(s) matching
-
-
-
- //go:embed
-which is a directive to embed files and folders into
-the application binary by the compiler.
-
-
-
-
-
-
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
-
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.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.
@@ -80,7 +67,7 @@ this single file, followed by variable
import (
- "embed"
+ //_ "embed"
+ "embed"
)
single_file_string
to access
@@ -88,15 +75,13 @@ this single file, followed by variable
-//go:embed embed-directive.sh
+//go:embed example_folder/single_file.txt
var single_file_string string
single_file_string
to access
-
[]byte
.
-Note that //go:embed
can only be used in package level,
-define it in any function will result in error.[]byte
.
@@ -104,17 +89,14 @@ define it in any function will result in error.
-//go:embed embed-directive.sh
+//go:embed example_folder/single_file.txt
var single_file_byte []byte
-
"//go:embed /path/to/folder"
-or with another example "//go:embed /path/to/folder/*"
. You can add multiple //go:embed
before defining a variable.
@@ -133,42 +115,45 @@ or with another example
-//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
-
embed-directive.sh
as string
.example_single_file.txt
as string
.
- println(single_file_string)
+ print(single_file_string)
-
embed-directive.sh
as []byte
.[]byte
.
- println(string(single_file_byte))
+ print(string(single_file_byte))
@@ -190,13 +175,32 @@ then print out the content.
-
embed-directive.hash
by reading the file from folder_FS
first,
-then print out the content.*.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.