Add embed-directive example (#423)
* Add embed-directive example and rename embedding to struct-embedding Signed-off-by: peterzhu1992 <peterzhu1992@gmail.com> * Minor tweaks Signed-off-by: peterzhu1992 <peterzhu1992@gmail.com> * Add some improvements Signed-off-by: peterzhu1992 <peterzhu1992@gmail.com> * Add isDir() checks for measure.go and generate.go in tools Signed-off-by: peterzhu1992 <peterzhu1992@gmail.com>
This commit is contained in:
45
examples/embed-directive/embed-directive.go
Normal file
45
examples/embed-directive/embed-directive.go
Normal file
@@ -0,0 +1,45 @@
|
||||
// `//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).
|
||||
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 (
|
||||
//_ "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
|
||||
|
||||
// Here is a similar example but include single file as `[]byte`.
|
||||
//go:embed example_folder/single_file.txt
|
||||
var single_file_byte []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
|
||||
|
||||
func main() {
|
||||
|
||||
// Print out content of `example_single_file.txt` as `string`.
|
||||
print(single_file_string)
|
||||
|
||||
// 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))
|
||||
|
||||
}
|
||||
2
examples/embed-directive/embed-directive.hash
Normal file
2
examples/embed-directive/embed-directive.hash
Normal file
@@ -0,0 +1,2 @@
|
||||
e996755d889f01c99c353616fb2bdeac6c3be6e6
|
||||
-EXJc75EbN-
|
||||
14
examples/embed-directive/embed-directive.sh
Normal file
14
examples/embed-directive/embed-directive.sh
Normal file
@@ -0,0 +1,14 @@
|
||||
# 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
|
||||
hello go
|
||||
hello go
|
||||
123
|
||||
456
|
||||
|
||||
1
examples/embed-directive/example_folder/multi_file1.hash
Normal file
1
examples/embed-directive/example_folder/multi_file1.hash
Normal file
@@ -0,0 +1 @@
|
||||
123
|
||||
1
examples/embed-directive/example_folder/multi_file2.hash
Normal file
1
examples/embed-directive/example_folder/multi_file2.hash
Normal file
@@ -0,0 +1 @@
|
||||
456
|
||||
1
examples/embed-directive/example_folder/single_file.txt
Normal file
1
examples/embed-directive/example_folder/single_file.txt
Normal file
@@ -0,0 +1 @@
|
||||
hello go
|
||||
Reference in New Issue
Block a user