Fix up embed-directive example and rename files to be shorter
This commit is contained in:
parent
d4bbdc258b
commit
fc88db1814
@ -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))
|
||||
}
|
||||
|
@ -1,2 +1,2 @@
|
||||
e996755d889f01c99c353616fb2bdeac6c3be6e6
|
||||
-EXJc75EbN-
|
||||
82ad92029645b4fd1b5ef0cd9df3fecdf0a6764e
|
||||
myu7kywm7oI
|
||||
|
@ -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
|
||||
|
85
public/embed-directive
generated
85
public/embed-directive
generated
@ -27,13 +27,14 @@
|
||||
|
||||
<tr>
|
||||
<td class="docs">
|
||||
<p><code>//go:embed</code> is a compiler directive that allows programs to include arbitrary
|
||||
files/folders in the binary. Read more about go directive
|
||||
<a href="https://dave.cheney.net/2018/01/08/gos-hidden-pragmas">here</a>.</p>
|
||||
<p><code>//go:embed</code> is a compiler directive that allows programs to include
|
||||
arbitrary files/folders in the binary at build time. Read more about go
|
||||
directives <a href="https://pkg.go.dev/cmd/compile#hdr-Compiler_Directives">here</a>
|
||||
and about the embed directive <a href="https://pkg.go.dev/embed">here</a>.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
<a href="http://play.golang.org/p/-EXJc75EbN-"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<a href="http://play.golang.org/p/myu7kywm7oI"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<pre class="chroma">
|
||||
<span class="kn">package</span> <span class="nx">main</span>
|
||||
</pre>
|
||||
@ -42,17 +43,15 @@ files/folders in the binary. Read more about go directive
|
||||
|
||||
<tr>
|
||||
<td class="docs">
|
||||
<p>If no exported identifiers is directly used from <code>embed</code> package,
|
||||
you can add an underscore <code>_</code> before the package name. In this example, we simply
|
||||
import it as we need to use the <code>embed.FS</code> type from the package.</p>
|
||||
<p>Import the <code>embed</code> package; if you don’t use any exported
|
||||
identifiers from this package, you can do a blank import with <code>_ "embed"</code>.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<pre class="chroma">
|
||||
<span class="kn">import</span> <span class="p">(</span>
|
||||
<span class="c1">//_ "embed"
|
||||
</span><span class="c1"></span> <span class="s">"embed"</span>
|
||||
<span class="s">"embed"</span>
|
||||
<span class="p">)</span>
|
||||
</pre>
|
||||
</td>
|
||||
@ -60,44 +59,47 @@ import it as we need to use the <code>embed.FS</code> type from the package.</p>
|
||||
|
||||
<tr>
|
||||
<td class="docs">
|
||||
<p>Since one file is defined after the directive, the compiler will only include
|
||||
this single file, followed by variable <code>single_file_string</code> to access as <code>string</code> type.</p>
|
||||
<p>embed directives accept paths relative to the directory containing the
|
||||
Go source file. This directive embeds the contents of the file into a
|
||||
<code>string</code> variable.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<pre class="chroma">
|
||||
<span class="c1">//go:embed example_folder/single_file.txt
|
||||
</span><span class="c1"></span><span class="kd">var</span> <span class="nx">single_file_string</span> <span class="kt">string</span>
|
||||
<span class="c1">//go:embed folder/single_file.txt
|
||||
</span><span class="c1"></span><span class="kd">var</span> <span class="nx">fileString</span> <span class="kt">string</span>
|
||||
</pre>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td class="docs">
|
||||
<p>Here is a similar example but include single file as <code>[]byte</code>.</p>
|
||||
<p>Or embed the contents of the file into a <code>[]byte</code>.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<pre class="chroma">
|
||||
<span class="c1">//go:embed example_folder/single_file.txt
|
||||
</span><span class="c1"></span><span class="kd">var</span> <span class="nx">single_file_byte</span> <span class="p">[]</span><span class="kt">byte</span>
|
||||
<span class="c1">//go:embed folder/single_file.txt
|
||||
</span><span class="c1"></span><span class="kd">var</span> <span class="nx">fileByte</span> <span class="p">[]</span><span class="kt">byte</span>
|
||||
</pre>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td class="docs">
|
||||
<p>We can also embed multiple files or even folders with wildcard.</p>
|
||||
<p>We can also embed multiple files or even folders with wildcards. This uses
|
||||
a variable of the <a href="https://pkg.go.dev/embed#FS">embed.FS type</a>, which
|
||||
implements a simple virtual file system.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<pre class="chroma">
|
||||
<span class="c1">//go:embed example_folder/single_file.txt
|
||||
</span><span class="c1">//go:embed example_folder/*.hash
|
||||
</span><span class="c1"></span><span class="kd">var</span> <span class="nx">folder_FS</span> <span class="nx">embed</span><span class="p">.</span><span class="nx">FS</span>
|
||||
<span class="c1">//go:embed folder/single_file.txt
|
||||
</span><span class="c1">//go:embed folder/*.hash
|
||||
</span><span class="c1"></span><span class="kd">var</span> <span class="nx">folder</span> <span class="nx">embed</span><span class="p">.</span><span class="nx">FS</span>
|
||||
</pre>
|
||||
</td>
|
||||
</tr>
|
||||
@ -115,45 +117,28 @@ this single file, followed by variable <code>single_file_string</code> to access
|
||||
|
||||
<tr>
|
||||
<td class="docs">
|
||||
<p>Print out content of <code>example_single_file.txt</code> as <code>string</code>.</p>
|
||||
<p>Print out contents of <code>single_file.txt</code>.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<pre class="chroma">
|
||||
<span class="nb">print</span><span class="p">(</span><span class="nx">single_file_string</span><span class="p">)</span>
|
||||
<span class="nb">print</span><span class="p">(</span><span class="nx">fileString</span><span class="p">)</span>
|
||||
<span class="nb">print</span><span class="p">(</span><span class="nb">string</span><span class="p">(</span><span class="nx">fileByte</span><span class="p">))</span>
|
||||
</pre>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td class="docs">
|
||||
<p>Now handle <code>[]byte</code>.</p>
|
||||
<p>Retrieve some files from the embedded folder.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<pre class="chroma">
|
||||
<span class="nb">print</span><span class="p">(</span><span class="nb">string</span><span class="p">(</span><span class="nx">single_file_byte</span><span class="p">))</span>
|
||||
</pre>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td class="docs">
|
||||
<p>Retrieve file(s) matching <code>*.hash</code> pattern by reading from variable <code>folder_FS</code> first,
|
||||
then print out.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<pre class="chroma">
|
||||
<span class="nx">hash_file1</span> <span class="o">:=</span> <span class="s">"example_folder/multi_file1.hash"</span>
|
||||
<span class="nx">hash_file2</span> <span class="o">:=</span> <span class="s">"example_folder/multi_file2.hash"</span>
|
||||
<span class="nx">hash_content1</span><span class="p">,</span> <span class="nx">_</span> <span class="o">:=</span> <span class="nx">folder_FS</span><span class="p">.</span><span class="nf">ReadFile</span><span class="p">(</span><span class="nx">hash_file1</span><span class="p">)</span>
|
||||
<span class="nx">hash_content2</span><span class="p">,</span> <span class="nx">_</span> <span class="o">:=</span> <span class="nx">folder_FS</span><span class="p">.</span><span class="nf">ReadFile</span><span class="p">(</span><span class="nx">hash_file2</span><span class="p">)</span>
|
||||
<span class="nb">print</span><span class="p">(</span><span class="nb">string</span><span class="p">(</span><span class="nx">hash_content1</span><span class="p">))</span>
|
||||
<span class="nb">print</span><span class="p">(</span><span class="nb">string</span><span class="p">(</span><span class="nx">hash_content2</span><span class="p">))</span>
|
||||
<span class="nx">content1</span><span class="p">,</span> <span class="nx">_</span> <span class="o">:=</span> <span class="nx">folder</span><span class="p">.</span><span class="nf">ReadFile</span><span class="p">(</span><span class="s">"folder/file1.hash"</span><span class="p">)</span>
|
||||
<span class="nb">print</span><span class="p">(</span><span class="nb">string</span><span class="p">(</span><span class="nx">content1</span><span class="p">))</span>
|
||||
</pre>
|
||||
</td>
|
||||
</tr>
|
||||
@ -164,7 +149,9 @@ then print out.</p>
|
||||
</td>
|
||||
<td class="code">
|
||||
|
||||
<pre class="chroma"><span class="p">}</span>
|
||||
<pre class="chroma"> <span class="nx">content2</span><span class="p">,</span> <span class="nx">_</span> <span class="o">:=</span> <span class="nx">folder</span><span class="p">.</span><span class="nf">ReadFile</span><span class="p">(</span><span class="s">"folder/file2.hash"</span><span class="p">)</span>
|
||||
<span class="nb">print</span><span class="p">(</span><span class="nb">string</span><span class="p">(</span><span class="nx">content2</span><span class="p">))</span>
|
||||
<span class="p">}</span>
|
||||
</pre>
|
||||
</td>
|
||||
</tr>
|
||||
@ -183,10 +170,10 @@ this example can only be run on your local machine.)</p>
|
||||
<td class="code leading">
|
||||
|
||||
<pre class="chroma">
|
||||
<span class="gp">$</span> mkdir -p example_folder
|
||||
<span class="gp">$</span> echo "hello go" > example_folder/single_file.txt
|
||||
<span class="gp">$</span> echo "123" > example_folder/multi_file1.hash
|
||||
<span class="gp">$</span> echo "456" > example_folder/multi_file2.hash</pre>
|
||||
<span class="gp">$</span> mkdir -p folder
|
||||
<span class="gp">$</span> echo "hello go" > folder/single_file.txt
|
||||
<span class="gp">$</span> echo "123" > folder/file1.hash
|
||||
<span class="gp">$</span> echo "456" > folder/file2.hash</pre>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
@ -219,7 +206,7 @@ this example can only be run on your local machine.)</p>
|
||||
</div>
|
||||
<script>
|
||||
var codeLines = [];
|
||||
codeLines.push('package main\u000A');codeLines.push('import (\u000A //_ \"embed\"\u000A \"embed\"\u000A)\u000A');codeLines.push('//go:embed example_folder/single_file.txt\u000Avar single_file_string string\u000A');codeLines.push('//go:embed example_folder/single_file.txt\u000Avar single_file_byte []byte\u000A');codeLines.push('//go:embed example_folder/single_file.txt\u000A//go:embed example_folder/*.hash\u000Avar folder_FS embed.FS\u000A');codeLines.push('func main() {\u000A');codeLines.push(' print(single_file_string)\u000A');codeLines.push(' print(string(single_file_byte))\u000A');codeLines.push(' hash_file1 :\u003D \"example_folder/multi_file1.hash\"\u000A hash_file2 :\u003D \"example_folder/multi_file2.hash\"\u000A hash_content1, _ :\u003D folder_FS.ReadFile(hash_file1)\u000A hash_content2, _ :\u003D folder_FS.ReadFile(hash_file2)\u000A print(string(hash_content1))\u000A print(string(hash_content2))\u000A');codeLines.push('}\u000A');codeLines.push('');codeLines.push('');
|
||||
codeLines.push('package main\u000A');codeLines.push('import (\u000A \"embed\"\u000A)\u000A');codeLines.push('//go:embed folder/single_file.txt\u000Avar fileString string\u000A');codeLines.push('//go:embed folder/single_file.txt\u000Avar fileByte []byte\u000A');codeLines.push('//go:embed folder/single_file.txt\u000A//go:embed folder/*.hash\u000Avar folder embed.FS\u000A');codeLines.push('func main() {\u000A');codeLines.push(' print(fileString)\u000A print(string(fileByte))\u000A');codeLines.push(' content1, _ :\u003D folder.ReadFile(\"folder/file1.hash\")\u000A print(string(content1))\u000A');codeLines.push(' content2, _ :\u003D folder.ReadFile(\"folder/file2.hash\")\u000A print(string(content2))\u000A}\u000A');codeLines.push('');codeLines.push('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
Loading…
x
Reference in New Issue
Block a user