parent
dad9113134
commit
80fb5ebddf
@ -18,19 +18,24 @@ func main() {
|
||||
|
||||
// Go signal notification works by sending `os.Signal`
|
||||
// values on a channel. We'll create a channel to
|
||||
// receive these notifications (we'll also make one to
|
||||
// notify us when the program can exit).
|
||||
// receive these notifications. Note that this channel
|
||||
// should be buffered.
|
||||
sigs := make(chan os.Signal, 1)
|
||||
done := make(chan bool, 1)
|
||||
|
||||
// `signal.Notify` registers the given channel to
|
||||
// receive notifications of the specified signals.
|
||||
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
||||
|
||||
// This goroutine executes a blocking receive for
|
||||
// signals. When it gets one it'll print it out
|
||||
// and then notify the program that it can finish.
|
||||
// We could receive from `sigs` here in the main
|
||||
// function, but let's see how this could also be
|
||||
// done in a separate goroutine, to demonstrate
|
||||
// a more realistic scenario of graceful shutdown.
|
||||
done := make(chan bool, 1)
|
||||
|
||||
go func() {
|
||||
// This goroutine executes a blocking receive for
|
||||
// signals. When it gets one it'll print it out
|
||||
// and then notify the program that it can finish.
|
||||
sig := <-sigs
|
||||
fmt.Println()
|
||||
fmt.Println(sig)
|
||||
|
@ -1,2 +1,2 @@
|
||||
ccee3fe41771b7cf56d64de38b12588022458154
|
||||
YRV64KEXJW1
|
||||
cd15508731199185f3205692af0f80cbdee4fcd7
|
||||
LauPuRo3v9l
|
||||
|
@ -66,3 +66,9 @@ func TestIntMinTableDriven(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkIntMin(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
IntMin(1, 2)
|
||||
}
|
||||
}
|
||||
|
@ -1,2 +1,2 @@
|
||||
c9ca6b71d9f762b689f1f08a490d8c7f7764fcb3
|
||||
vY8PN0c6BSx
|
||||
25e8941d63b555a590e6d44a95ae0e41ecadadca
|
||||
ALL2BVLkYEr
|
||||
|
40
public/signals
generated
40
public/signals
generated
@ -46,7 +46,7 @@ Here’s how to handle signals in Go with channels.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
<a href="http://play.golang.org/p/YRV64KEXJW1"><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/LauPuRo3v9l"><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>
|
||||
@ -83,15 +83,14 @@ Here’s how to handle signals in Go with channels.</p>
|
||||
<td class="docs">
|
||||
<p>Go signal notification works by sending <code>os.Signal</code>
|
||||
values on a channel. We’ll create a channel to
|
||||
receive these notifications (we’ll also make one to
|
||||
notify us when the program can exit).</p>
|
||||
receive these notifications. Note that this channel
|
||||
should be buffered.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<pre class="chroma">
|
||||
<span class="nx">sigs</span> <span class="o">:=</span> <span class="nb">make</span><span class="p">(</span><span class="kd">chan</span> <span class="nx">os</span><span class="p">.</span><span class="nx">Signal</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
|
||||
<span class="nx">done</span> <span class="o">:=</span> <span class="nb">make</span><span class="p">(</span><span class="kd">chan</span> <span class="kt">bool</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
|
||||
</pre>
|
||||
</td>
|
||||
</tr>
|
||||
@ -110,6 +109,22 @@ receive notifications of the specified signals.</p>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td class="docs">
|
||||
<p>We could receive from <code>sigs</code> here in the main
|
||||
function, but let’s see how this could also be
|
||||
done in a separate goroutine, to demonstrate
|
||||
a more realistic scenario of graceful shutdown.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<pre class="chroma">
|
||||
<span class="nx">done</span> <span class="o">:=</span> <span class="nb">make</span><span class="p">(</span><span class="kd">chan</span> <span class="kt">bool</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
|
||||
</pre>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td class="docs">
|
||||
<p>This goroutine executes a blocking receive for
|
||||
@ -119,9 +134,18 @@ and then notify the program that it can finish.</p>
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<pre class="chroma">
|
||||
<span class="k">go</span> <span class="kd">func</span><span class="p">()</span> <span class="p">{</span>
|
||||
<span class="nx">sig</span> <span class="o">:=</span> <span class="o"><-</span><span class="nx">sigs</span>
|
||||
<pre class="chroma"> <span class="k">go</span> <span class="kd">func</span><span class="p">()</span> <span class="p">{</span>
|
||||
</pre>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td class="docs">
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<pre class="chroma"> <span class="nx">sig</span> <span class="o">:=</span> <span class="o"><-</span><span class="nx">sigs</span>
|
||||
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">()</span>
|
||||
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="nx">sig</span><span class="p">)</span>
|
||||
<span class="nx">done</span> <span class="o"><-</span> <span class="kc">true</span>
|
||||
@ -186,7 +210,7 @@ causing the program to print <code>interrupt</code> and then exit.</p>
|
||||
</div>
|
||||
<script>
|
||||
var codeLines = [];
|
||||
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"os\"\u000A \"os/signal\"\u000A \"syscall\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' sigs :\u003D make(chan os.Signal, 1)\u000A done :\u003D make(chan bool, 1)\u000A');codeLines.push(' signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)\u000A');codeLines.push(' go func() {\u000A sig :\u003D \u003C-sigs\u000A fmt.Println()\u000A fmt.Println(sig)\u000A done \u003C- true\u000A }()\u000A');codeLines.push(' fmt.Println(\"awaiting signal\")\u000A \u003C-done\u000A fmt.Println(\"exiting\")\u000A}\u000A');codeLines.push('');
|
||||
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"os\"\u000A \"os/signal\"\u000A \"syscall\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' sigs :\u003D make(chan os.Signal, 1)\u000A');codeLines.push(' signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)\u000A');codeLines.push(' done :\u003D make(chan bool, 1)\u000A');codeLines.push(' go func() {\u000A');codeLines.push(' sig :\u003D \u003C-sigs\u000A fmt.Println()\u000A fmt.Println(sig)\u000A done \u003C- true\u000A }()\u000A');codeLines.push(' fmt.Println(\"awaiting signal\")\u000A \u003C-done\u000A fmt.Println(\"exiting\")\u000A}\u000A');codeLines.push('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
21
public/testing
generated
21
public/testing
generated
@ -47,7 +47,7 @@ typically lives in the same package as the code it tests.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
<a href="http://play.golang.org/p/vY8PN0c6BSx"><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/ALL2BVLkYEr"><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>
|
||||
@ -167,7 +167,7 @@ when executing <code>go test -v</code>.</p>
|
||||
<td class="docs">
|
||||
|
||||
</td>
|
||||
<td class="code">
|
||||
<td class="code leading">
|
||||
|
||||
<pre class="chroma"> <span class="nx">testname</span> <span class="o">:=</span> <span class="nx">fmt</span><span class="p">.</span><span class="nf">Sprintf</span><span class="p">(</span><span class="s">"%d,%d"</span><span class="p">,</span> <span class="nx">tt</span><span class="p">.</span><span class="nx">a</span><span class="p">,</span> <span class="nx">tt</span><span class="p">.</span><span class="nx">b</span><span class="p">)</span>
|
||||
<span class="nx">t</span><span class="p">.</span><span class="nf">Run</span><span class="p">(</span><span class="nx">testname</span><span class="p">,</span> <span class="kd">func</span><span class="p">(</span><span class="nx">t</span> <span class="o">*</span><span class="nx">testing</span><span class="p">.</span><span class="nx">T</span><span class="p">)</span> <span class="p">{</span>
|
||||
@ -178,6 +178,21 @@ when executing <code>go test -v</code>.</p>
|
||||
<span class="p">})</span>
|
||||
<span class="p">}</span>
|
||||
<span class="p">}</span>
|
||||
</pre>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td class="docs">
|
||||
|
||||
</td>
|
||||
<td class="code">
|
||||
|
||||
<pre class="chroma"><span class="kd">func</span> <span class="nf">BenchmarkIntMin</span><span class="p">(</span><span class="nx">b</span> <span class="o">*</span><span class="nx">testing</span><span class="p">.</span><span class="nx">B</span><span class="p">)</span> <span class="p">{</span>
|
||||
<span class="k">for</span> <span class="nx">i</span> <span class="o">:=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="p"><</span> <span class="nx">b</span><span class="p">.</span><span class="nx">N</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span> <span class="p">{</span>
|
||||
<span class="nf">IntMin</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
|
||||
<span class="p">}</span>
|
||||
<span class="p">}</span>
|
||||
</pre>
|
||||
</td>
|
||||
</tr>
|
||||
@ -229,7 +244,7 @@ when executing <code>go test -v</code>.</p>
|
||||
</div>
|
||||
<script>
|
||||
var codeLines = [];
|
||||
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"testing\"\u000A)\u000A');codeLines.push('func IntMin(a, b int) int {\u000A if a \u003C b {\u000A return a\u000A }\u000A return b\u000A}\u000A');codeLines.push('func TestIntMinBasic(t *testing.T) {\u000A ans :\u003D IntMin(2, -2)\u000A if ans !\u003D -2 {\u000A');codeLines.push(' t.Errorf(\"IntMin(2, -2) \u003D %d; want -2\", ans)\u000A }\u000A}\u000A');codeLines.push('func TestIntMinTableDriven(t *testing.T) {\u000A var tests \u003D []struct {\u000A a, b int\u000A want int\u000A }{\u000A {0, 1, 0},\u000A {1, 0, 0},\u000A {2, -2, -2},\u000A {0, -1, -1},\u000A {-1, 0, -1},\u000A }\u000A');codeLines.push(' for _, tt :\u003D range tests {\u000A');codeLines.push(' testname :\u003D fmt.Sprintf(\"%d,%d\", tt.a, tt.b)\u000A t.Run(testname, func(t *testing.T) {\u000A ans :\u003D IntMin(tt.a, tt.b)\u000A if ans !\u003D tt.want {\u000A t.Errorf(\"got %d, want %d\", ans, tt.want)\u000A }\u000A })\u000A }\u000A}\u000A');codeLines.push('');
|
||||
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"testing\"\u000A)\u000A');codeLines.push('func IntMin(a, b int) int {\u000A if a \u003C b {\u000A return a\u000A }\u000A return b\u000A}\u000A');codeLines.push('func TestIntMinBasic(t *testing.T) {\u000A ans :\u003D IntMin(2, -2)\u000A if ans !\u003D -2 {\u000A');codeLines.push(' t.Errorf(\"IntMin(2, -2) \u003D %d; want -2\", ans)\u000A }\u000A}\u000A');codeLines.push('func TestIntMinTableDriven(t *testing.T) {\u000A var tests \u003D []struct {\u000A a, b int\u000A want int\u000A }{\u000A {0, 1, 0},\u000A {1, 0, 0},\u000A {2, -2, -2},\u000A {0, -1, -1},\u000A {-1, 0, -1},\u000A }\u000A');codeLines.push(' for _, tt :\u003D range tests {\u000A');codeLines.push(' testname :\u003D fmt.Sprintf(\"%d,%d\", tt.a, tt.b)\u000A t.Run(testname, func(t *testing.T) {\u000A ans :\u003D IntMin(tt.a, tt.b)\u000A if ans !\u003D tt.want {\u000A t.Errorf(\"got %d, want %d\", ans, tt.want)\u000A }\u000A })\u000A }\u000A}\u000A');codeLines.push('func BenchmarkIntMin(b *testing.B) {\u000A for i :\u003D 0; i \u003C b.N; i++ {\u000A IntMin(1, 2)\u000A }\u000A}\u000A');codeLines.push('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
Loading…
x
Reference in New Issue
Block a user