Hana 9e216da9ef go.mod: add go.mod and move pygments to third_party
After go1.16, go will use module mode by default,
even when the repository is checked out under GOPATH
or in a one-off directory. Add go.mod, go.sum to keep
this repo buildable without opting out of the module
mode.

> go mod init github.com/mmcgrana/gobyexample
> go mod tidy
> go mod vendor

In module mode, the 'vendor' directory is special
and its contents will be actively maintained by the
go command. pygments aren't the dependency the go will
know about, so it will delete the contents from vendor
directory. Move it to `third_party` directory now.

And, vendor the blackfriday package.

Note: the tutorial contents are not affected by the
change in go1.16 because all the examples in this
tutorial ask users to run the go command with the
explicit list of files to be compiled (e.g.
`go run hello-world.go` or `go build command-line-arguments.go`).
When the source list is provided, the go command does
not have to compute the build list and whether it's
running in GOPATH mode or module mode becomes irrelevant.
2021-02-15 16:45:26 -05:00

213 lines
9.3 KiB
Plaintext
Generated

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Go by Example: Context</title>
<link rel=stylesheet href="site.css">
</head>
<script>
onkeydown = (e) => {
if (e.key == "ArrowLeft") {
window.location.href = 'http-servers';
}
if (e.key == "ArrowRight") {
window.location.href = 'spawning-processes';
}
}
</script>
<body>
<div class="example" id="context">
<h2><a href="./">Go by Example</a>: Context</h2>
<table>
<tr>
<td class="docs">
<p>In the previous example we looked at setting up a simple
<a href="http-servers">HTTP server</a>. HTTP servers are useful for
demonstrating the usage of <code>context.Context</code> for
controlling cancellation. A <code>Context</code> carries deadlines,
cancellation signals, and other request-scoped values
across API boundaries and goroutines.</p>
</td>
<td class="code leading">
<a href="http://play.golang.org/p/0_bu1o8rIBO"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<div class="highlight"><pre><span class="kn">import</span> <span class="p">(</span>
<span class="s">&quot;fmt&quot;</span>
<span class="s">&quot;net/http&quot;</span>
<span class="s">&quot;time&quot;</span>
<span class="p">)</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<div class="highlight"><pre><span class="kd">func</span> <span class="nx">hello</span><span class="p">(</span><span class="nx">w</span> <span class="nx">http</span><span class="p">.</span><span class="nx">ResponseWriter</span><span class="p">,</span> <span class="nx">req</span> <span class="o">*</span><span class="nx">http</span><span class="p">.</span><span class="nx">Request</span><span class="p">)</span> <span class="p">{</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
<p>A <code>context.Context</code> is created for each request by
the <code>net/http</code> machinery, and is available with
the <code>Context()</code> method.</p>
</td>
<td class="code leading">
<div class="highlight"><pre> <span class="nx">ctx</span> <span class="o">:=</span> <span class="nx">req</span><span class="p">.</span><span class="nx">Context</span><span class="p">()</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="s">&quot;server: hello handler started&quot;</span><span class="p">)</span>
<span class="k">defer</span> <span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="s">&quot;server: hello handler ended&quot;</span><span class="p">)</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
<p>Wait for a few seconds before sending a reply to the
client. This could simulate some work the server is
doing. While working, keep an eye on the context&rsquo;s
<code>Done()</code> channel for a signal that we should cancel
the work and return as soon as possible.</p>
</td>
<td class="code leading">
<div class="highlight"><pre> <span class="k">select</span> <span class="p">{</span>
<span class="k">case</span> <span class="o">&lt;-</span><span class="nx">time</span><span class="p">.</span><span class="nx">After</span><span class="p">(</span><span class="mi">10</span> <span class="o">*</span> <span class="nx">time</span><span class="p">.</span><span class="nx">Second</span><span class="p">):</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Fprintf</span><span class="p">(</span><span class="nx">w</span><span class="p">,</span> <span class="s">&quot;hello\n&quot;</span><span class="p">)</span>
<span class="k">case</span> <span class="o">&lt;-</span><span class="nx">ctx</span><span class="p">.</span><span class="nx">Done</span><span class="p">():</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
<p>The context&rsquo;s <code>Err()</code> method returns an error
that explains why the <code>Done()</code> channel was
closed.</p>
</td>
<td class="code leading">
<div class="highlight"><pre> <span class="nx">err</span> <span class="o">:=</span> <span class="nx">ctx</span><span class="p">.</span><span class="nx">Err</span><span class="p">()</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="s">&quot;server:&quot;</span><span class="p">,</span> <span class="nx">err</span><span class="p">)</span>
<span class="nx">internalError</span> <span class="o">:=</span> <span class="nx">http</span><span class="p">.</span><span class="nx">StatusInternalServerError</span>
<span class="nx">http</span><span class="p">.</span><span class="nx">Error</span><span class="p">(</span><span class="nx">w</span><span class="p">,</span> <span class="nx">err</span><span class="p">.</span><span class="nx">Error</span><span class="p">(),</span> <span class="nx">internalError</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<div class="highlight"><pre><span class="kd">func</span> <span class="nx">main</span><span class="p">()</span> <span class="p">{</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
<p>As before, we register our handler on the &ldquo;/hello&rdquo;
route, and start serving.</p>
</td>
<td class="code">
<div class="highlight"><pre> <span class="nx">http</span><span class="p">.</span><span class="nx">HandleFunc</span><span class="p">(</span><span class="s">&quot;/hello&quot;</span><span class="p">,</span> <span class="nx">hello</span><span class="p">)</span>
<span class="nx">http</span><span class="p">.</span><span class="nx">ListenAndServe</span><span class="p">(</span><span class="s">&quot;:8090&quot;</span><span class="p">,</span> <span class="kc">nil</span><span class="p">)</span>
<span class="p">}</span>
</pre></div>
</td>
</tr>
</table>
<table>
<tr>
<td class="docs">
<p>Run the server in the background.</p>
</td>
<td class="code leading">
<div class="highlight"><pre><span class="gp">$</span> go run context-in-http-servers.go <span class="p">&amp;</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
<p>Simulate a client request to <code>/hello</code>, hitting
Ctrl+C shortly after starting to signal
cancellation.</p>
</td>
<td class="code">
<div class="highlight"><pre><span class="gp">$</span> curl localhost:8090/hello
<span class="go">server: hello handler started</span>
<span class="go">^C</span>
<span class="go">server: context canceled</span>
<span class="go">server: hello handler ended</span>
</pre></div>
</td>
</tr>
</table>
<p class="next">
Next example: <a href="spawning-processes">Spawning Processes</a>.
</p>
<p class="footer">
by <a href="https://markmcgranaghan.com">Mark McGranaghan</a> | <a href="https://github.com/mmcgrana/gobyexample/blob/master/examples/context">source</a> | <a href="https://github.com/mmcgrana/gobyexample#license">license</a>
</p>
</div>
<script>
var codeLines = [];
codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"net/http\"\u000A \"time\"\u000A)\u000A');codeLines.push('func hello(w http.ResponseWriter, req *http.Request) {\u000A');codeLines.push(' ctx :\u003D req.Context()\u000A fmt.Println(\"server: hello handler started\")\u000A defer fmt.Println(\"server: hello handler ended\")\u000A');codeLines.push(' select {\u000A case \u003C-time.After(10 * time.Second):\u000A fmt.Fprintf(w, \"hello\\n\")\u000A case \u003C-ctx.Done():\u000A');codeLines.push(' err :\u003D ctx.Err()\u000A fmt.Println(\"server:\", err)\u000A internalError :\u003D http.StatusInternalServerError\u000A http.Error(w, err.Error(), internalError)\u000A }\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' http.HandleFunc(\"/hello\", hello)\u000A http.ListenAndServe(\":8090\", nil)\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>
</html>