Add embed-directive example and rename embedding to struct-embedding

Signed-off-by: peterzhu1992 <peterzhu1992@gmail.com>
This commit is contained in:
peterzhu1992 2022-05-17 01:20:06 -04:00
parent 9deadb76ae
commit f900918c93
No known key found for this signature in database
GPG Key ID: AE1C56C7D03DDA1C
13 changed files with 302 additions and 14 deletions

View File

@ -19,7 +19,8 @@ Strings and Runes
Structs
Methods
Interfaces
Embedding
Struct Embedding
Embed Directive
Generics
Errors
Goroutines

View File

@ -0,0 +1,51 @@
// Starting in Go version 1.16+, Go supports use of `//go:embed`
// which is a directive to embed files and folders into
// the application binary by the compiler.
// 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
// If you are not using any exported identifiers from `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.
import (
"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 embed-directive.sh
var single_file_string string
// Here is a similar example but include single file as `[]byte`.
// Note that `//go:embed` can only be used in package level,
// define it in any function will result in error.
//go:embed embed-directive.sh
var single_file_byte []byte
// 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 `"//go:embed /path/to/folder"`
// 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
func main() {
// Print out the content of `embed-directive.sh` as `string`.
println(single_file_string)
// Print out the content of `embed-directive.sh` as `[]byte`.
println(string(single_file_byte))
// Print out the content of `embed-directive.hash` by reading the file from `folder_FS` first,
// then print out the content.
hash_file := "embed-directive.hash"
hash_content, _ := folder_FS.ReadFile(hash_file)
println(string(hash_content))
}

View File

@ -0,0 +1,2 @@
10914400b16498094a1a876856181673c3fcdfe0
gk3Zh5Sx8rV

View File

@ -0,0 +1,4 @@
# Use these commands to run the example
$ go run embed-directive.go

View File

@ -1,2 +0,0 @@
316e334f61f03d59c8a45889a057903a786534ba
k5Z_CnP8DK9

View File

@ -1,5 +1,8 @@
// Go supports _embedding_ of structs and interfaces
// to express a more seamless _composition_ of types.
// This is not to be confused with _go:embed_ which is
// a go directive introduced in go version 1.16 to embed
// files and folders into the application binary.
package main

View File

@ -0,0 +1,2 @@
8ec60ccaf4c5803bd65a776e05b458b008ae52a7
4B33kwDJ3fm

222
public/embed-directive generated Normal file
View File

@ -0,0 +1,222 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Go by Example: Embed Directive</title>
<link rel=stylesheet href="site.css">
</head>
<script>
onkeydown = (e) => {
if (e.key == "ArrowLeft") {
window.location.href = 'struct-embedding';
}
if (e.key == "ArrowRight") {
window.location.href = 'generics';
}
}
</script>
<body>
<div class="example" id="embed-directive">
<h2><a href="./">Go by Example</a>: Embed Directive</h2>
<table>
<tr>
<td class="docs">
<p>Starting in Go version 1.16+, Go supports use of <code>//go:embed</code>
which is a directive to embed files and folders into
the application binary by the compiler.</p>
</td>
<td class="code empty leading">
</td>
</tr>
<tr>
<td class="docs">
<p>Since Go does not have preprocessor or macros like C type of languages,
<code>pragmas/directives</code> are implemented by the compiler as comments <code>//</code>.
Using <code>//go:embed</code> tells the compiler to include arbitrary files/folders in binary.</p>
</td>
<td class="code leading">
<a href="http://play.golang.org/p/gk3Zh5Sx8rV"><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>
</td>
</tr>
<tr>
<td class="docs">
<p>If you are not using any exported identifiers from <code>embed</code> package directly,
you can add an underscore <code>_</code> before the package name, ask Go to import <code>embed</code>
without directly using it, like this: <code>_ &quot;embed&quot;</code>. In this example, we remove the
underscore as we need to use the <code>embed.FS</code> type from the package.</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="kn">import</span> <span class="p">(</span>
<span class="s">&#34;embed&#34;</span>
<span class="p">)</span>
</pre>
</td>
</tr>
<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>
</td>
<td class="code leading">
<pre class="chroma">
<span class="c1">//go:embed embed-directive.sh
</span><span class="c1"></span><span class="kd">var</span> <span class="nx">single_file_string</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>.
Note that <code>//go:embed</code> can only be used in package level,
define it in any function will result in error.</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="c1">//go:embed embed-directive.sh
</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>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>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 <code>&quot;//go:embed /path/to/folder&quot;</code>
or with another example <code>&quot;//go:embed /path/to/folder/*&quot;</code>. You can add multiple <code>//go:embed</code> before defining a variable.</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="c1">//go:embed *.hash
</span><span class="c1">//go:embed embed-directive.sh
</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>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"><span class="kd">func</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>Print out the content of <code>embed-directive.sh</code> as <code>string</code>.</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nb">println</span><span class="p">(</span><span class="nx">single_file_string</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>Print out the content of <code>embed-directive.sh</code> as <code>[]byte</code>.</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nb">println</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>Print out the content of <code>embed-directive.hash</code> by reading the file from <code>folder_FS</code> first,
then print out the content.</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">hash_file</span> <span class="o">:=</span> <span class="s">&#34;embed-directive.hash&#34;</span>
<span class="nx">hash_content</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_file</span><span class="p">)</span>
<span class="nb">println</span><span class="p">(</span><span class="nb">string</span><span class="p">(</span><span class="nx">hash_content</span><span class="p">))</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code">
<pre class="chroma"><span class="p">}</span>
</pre>
</td>
</tr>
</table>
<table>
<tr>
<td class="docs">
<p>Use these commands to run the example</p>
</td>
<td class="code">
<pre class="chroma">
<span class="gp">$</span> go run embed-directive.go</pre>
</td>
</tr>
</table>
<p class="next">
Next example: <a href="generics">Generics</a>.
</p>
<p class="footer">
by <a href="https://markmcgranaghan.com">Mark McGranaghan</a> and <a href="https://eli.thegreenplace.net">Eli Bendersky</a> | <a href="https://github.com/mmcgrana/gobyexample">source</a> | <a href="https://github.com/mmcgrana/gobyexample#license">license</a>
</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"embed\"\u000A)\u000A');codeLines.push('//go:embed embed-directive.sh\u000Avar single_file_string string\u000A');codeLines.push('//go:embed embed-directive.sh\u000Avar single_file_byte []byte\u000A');codeLines.push('//go:embed *.hash\u000A//go:embed embed-directive.sh\u000Avar folder_FS embed.FS\u000A');codeLines.push('func main() {\u000A');codeLines.push(' println(single_file_string)\u000A');codeLines.push(' println(string(single_file_byte))\u000A');codeLines.push(' hash_file :\u003D \"embed-directive.hash\"\u000A hash_content, _ :\u003D folder_FS.ReadFile(hash_file)\u000A println(string(hash_content))\u000A');codeLines.push('}\u000A');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>
</html>

2
public/generics generated
View File

@ -9,7 +9,7 @@
onkeydown = (e) => {
if (e.key == "ArrowLeft") {
window.location.href = 'embedding';
window.location.href = 'embed-directive';
}

4
public/index.html generated
View File

@ -69,7 +69,9 @@
<li><a href="interfaces">Interfaces</a></li>
<li><a href="embedding">Embedding</a></li>
<li><a href="struct-embedding">Struct Embedding</a></li>
<li><a href="embed-directive">Embed Directive</a></li>
<li><a href="generics">Generics</a></li>

4
public/interfaces generated
View File

@ -14,7 +14,7 @@
if (e.key == "ArrowRight") {
window.location.href = 'embedding';
window.location.href = 'struct-embedding';
}
}
@ -222,7 +222,7 @@ these structs as arguments to <code>measure</code>.</p>
<p class="next">
Next example: <a href="embedding">Embedding</a>.
Next example: <a href="struct-embedding">Struct Embedding</a>.
</p>

View File

@ -2,7 +2,7 @@
<html>
<head>
<meta charset="utf-8">
<title>Go by Example: Embedding</title>
<title>Go by Example: Struct Embedding</title>
<link rel=stylesheet href="site.css">
</head>
<script>
@ -14,21 +14,24 @@
if (e.key == "ArrowRight") {
window.location.href = 'generics';
window.location.href = 'embed-directive';
}
}
</script>
<body>
<div class="example" id="embedding">
<h2><a href="./">Go by Example</a>: Embedding</h2>
<div class="example" id="struct-embedding">
<h2><a href="./">Go by Example</a>: Struct Embedding</h2>
<table>
<tr>
<td class="docs">
<p>Go supports <em>embedding</em> of structs and interfaces
to express a more seamless <em>composition</em> of types.</p>
to express a more seamless <em>composition</em> of types.
This is not to be confused with <em>go:embed</em> which is
a go directive introduced in go version 1.16 to embed
files and folders into the application binary.</p>
</td>
<td class="code empty leading">
@ -42,7 +45,7 @@ to express a more seamless <em>composition</em> of types.</p>
</td>
<td class="code leading">
<a href="http://play.golang.org/p/k5Z_CnP8DK9"><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/4B33kwDJ3fm"><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>
</td>
@ -230,7 +233,7 @@ we see that a <code>container</code> now implements the
<p class="next">
Next example: <a href="generics">Generics</a>.
Next example: <a href="embed-directive">Embed Directive</a>.
</p>