Add some improvements
Signed-off-by: peterzhu1992 <peterzhu1992@gmail.com>
This commit is contained in:
parent
40a68991d7
commit
40ac3bff37
@ -20,7 +20,6 @@ Structs
|
|||||||
Methods
|
Methods
|
||||||
Interfaces
|
Interfaces
|
||||||
Struct Embedding
|
Struct Embedding
|
||||||
Embed Directive
|
|
||||||
Generics
|
Generics
|
||||||
Errors
|
Errors
|
||||||
Goroutines
|
Goroutines
|
||||||
@ -66,6 +65,7 @@ Line Filters
|
|||||||
File Paths
|
File Paths
|
||||||
Directories
|
Directories
|
||||||
Temporary Files and Directories
|
Temporary Files and Directories
|
||||||
|
Embed Directive
|
||||||
Testing and Benchmarking
|
Testing and Benchmarking
|
||||||
Command-Line Arguments
|
Command-Line Arguments
|
||||||
Command-Line Flags
|
Command-Line Flags
|
||||||
|
@ -1,51 +1,45 @@
|
|||||||
// Starting in Go version 1.16+, Go supports use of `//go:embed`
|
// `//go:embed` is a compiler directive that allows programs to include arbitrary
|
||||||
// which is a directive to embed files and folders into
|
// files/folders in the binary. Read more about go directive
|
||||||
// the application binary by the compiler.
|
// [here](https://dave.cheney.net/2018/01/08/gos-hidden-pragmas).
|
||||||
|
|
||||||
// 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.
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
// If you are not using any exported identifiers from `embed` package directly,
|
// If no exported identifiers is directly used from `embed` package,
|
||||||
// you can add an underscore `_` before the package name, ask Go to import `embed`
|
// you can add an underscore `_` before the package name. In this example, we simply
|
||||||
// without directly using it, like this: `_ "embed"`. In this example, we remove the
|
// import it as we need to use the `embed.FS` type from the package.
|
||||||
// underscore as we need to use the `embed.FS` type from the package.
|
|
||||||
import (
|
import (
|
||||||
|
//_ "embed"
|
||||||
"embed"
|
"embed"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Since one file is defined after the directive, the compiler will only include
|
// 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.
|
// this single file, followed by variable `single_file_string` to access as `string` type.
|
||||||
//go:embed embed-directive.sh
|
//go:embed example_folder/single_file.txt
|
||||||
var single_file_string string
|
var single_file_string string
|
||||||
|
|
||||||
// Here is a similar example but include single file as `[]byte`.
|
// Here is a similar example but include single file as `[]byte`.
|
||||||
// Note that `//go:embed` can only be used in package level,
|
//go:embed example_folder/single_file.txt
|
||||||
// define it in any function will result in error.
|
|
||||||
//go:embed embed-directive.sh
|
|
||||||
var single_file_byte []byte
|
var single_file_byte []byte
|
||||||
|
|
||||||
// We can also embed multiple files or even folders with wildcard.
|
// 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
|
//go:embed example_folder/single_file.txt
|
||||||
// current working directory as examples. In practice, you can embed files with `"//go:embed /path/to/folder"`
|
//go:embed example_folder/*.hash
|
||||||
// or with another example `"//go:embed /path/to/folder/*"`. You can add multiple `//go:embed` before defining a variable.
|
|
||||||
//go:embed *.hash
|
|
||||||
//go:embed embed-directive.sh
|
|
||||||
var folder_FS embed.FS
|
var folder_FS embed.FS
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
// Print out the content of `embed-directive.sh` as `string`.
|
// Print out the 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,
|
// Retrieve the file(s) matching `*.hash` pattern by reading from `folder_FS` first,
|
||||||
// then print out the content.
|
// then print out.
|
||||||
hash_file := "embed-directive.hash"
|
hash_file1 := "example_folder/multi_file1.hash"
|
||||||
hash_content, _ := folder_FS.ReadFile(hash_file)
|
hash_file2 := "example_folder/multi_file2.hash"
|
||||||
println(string(hash_content))
|
hash_content1, _ := folder_FS.ReadFile(hash_file1)
|
||||||
|
hash_content2, _ := folder_FS.ReadFile(hash_file2)
|
||||||
|
print(string(hash_content1))
|
||||||
|
print(string(hash_content2))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,2 +1,2 @@
|
|||||||
10914400b16498094a1a876856181673c3fcdfe0
|
33356517e565ff2d1f9d7f935012a326e8f5b3ea
|
||||||
gk3Zh5Sx8rV
|
yTsQsPe9jth
|
||||||
|
@ -1,4 +1,15 @@
|
|||||||
# 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
|
||||||
|
|
||||||
|
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
|
Loading…
x
Reference in New Issue
Block a user