gobyexample/public/line-filters
Oleg Butuzov 646c8b39da bugfix (mmcgrana/gobyexample#294) sha1 sum calculator
This bugfix implements correct way to calculate source sha1 hash, before it will changed,
by propagation of unchanged sources.

This commit will also include regenerated static files and *.hash files.
2019-10-22 14:27:02 +03:00

212 lines
7.6 KiB
Plaintext
Generated

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Go by Example: Line Filters</title>
<link rel=stylesheet href="site.css">
</head>
<script>
onkeydown = (e) => {
if (e.key == "ArrowLeft") {
window.location.href = 'writing-files';
}
if (e.key == "ArrowRight") {
window.location.href = 'file-paths';
}
}
</script>
<body>
<div class="example" id="line-filters">
<h2><a href="./">Go by Example</a>: Line Filters</h2>
<table>
<tr>
<td class="docs">
<p>A <em>line filter</em> is a common type of program that reads
input on stdin, processes it, and then prints some
derived result to stdout. <code>grep</code> and <code>sed</code> are common
line filters.</p>
</td>
<td class="code empty leading">
</td>
</tr>
<tr>
<td class="docs">
<p>Here&rsquo;s an example line filter in Go that writes a
capitalized version of all input text. You can use this
pattern to write your own Go line filters.</p>
</td>
<td class="code leading">
<a href="http://play.golang.org/p/kNcupWRsYPP"><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;bufio&quot;</span>
<span class="s">&quot;fmt&quot;</span>
<span class="s">&quot;os&quot;</span>
<span class="s">&quot;strings&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">main</span><span class="p">()</span> <span class="p">{</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
<p>Wrapping the unbuffered <code>os.Stdin</code> with a buffered
scanner gives us a convenient <code>Scan</code> method that
advances the scanner to the next token; which is
the next line in the default scanner.</p>
</td>
<td class="code leading">
<div class="highlight"><pre> <span class="nx">scanner</span> <span class="o">:=</span> <span class="nx">bufio</span><span class="p">.</span><span class="nx">NewScanner</span><span class="p">(</span><span class="nx">os</span><span class="p">.</span><span class="nx">Stdin</span><span class="p">)</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
<p><code>Text</code> returns the current token, here the next line,
from the input.</p>
</td>
<td class="code leading">
<div class="highlight"><pre> <span class="k">for</span> <span class="nx">scanner</span><span class="p">.</span><span class="nx">Scan</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="nx">ucl</span> <span class="o">:=</span> <span class="nx">strings</span><span class="p">.</span><span class="nx">ToUpper</span><span class="p">(</span><span class="nx">scanner</span><span class="p">.</span><span class="nx">Text</span><span class="p">())</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
<p>Write out the uppercased line.</p>
</td>
<td class="code leading">
<div class="highlight"><pre> <span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="nx">ucl</span><span class="p">)</span>
<span class="p">}</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
<p>Check for errors during <code>Scan</code>. End of file is
expected and not reported by <code>Scan</code> as an error.</p>
</td>
<td class="code">
<div class="highlight"><pre> <span class="k">if</span> <span class="nx">err</span> <span class="o">:=</span> <span class="nx">scanner</span><span class="p">.</span><span class="nx">Err</span><span class="p">();</span> <span class="nx">err</span> <span class="o">!=</span> <span class="kc">nil</span> <span class="p">{</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Fprintln</span><span class="p">(</span><span class="nx">os</span><span class="p">.</span><span class="nx">Stderr</span><span class="p">,</span> <span class="s">&quot;error:&quot;</span><span class="p">,</span> <span class="nx">err</span><span class="p">)</span>
<span class="nx">os</span><span class="p">.</span><span class="nx">Exit</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
</td>
</tr>
</table>
<table>
<tr>
<td class="docs">
<p>To try out our line filter, first make a file with a few
lowercase lines.</p>
</td>
<td class="code leading">
<div class="highlight"><pre><span class="gp">$</span> <span class="nb">echo</span> <span class="s1">&#39;hello&#39;</span> &gt; /tmp/lines
<span class="gp">$</span> <span class="nb">echo</span> <span class="s1">&#39;filter&#39;</span> &gt;&gt; /tmp/lines
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
<p>Then use the line filter to get uppercase lines.</p>
</td>
<td class="code">
<div class="highlight"><pre><span class="gp">$</span> cat /tmp/lines <span class="p">|</span> go run line-filters.go
<span class="go">HELLO</span>
<span class="go">FILTER</span>
</pre></div>
</td>
</tr>
</table>
<p class="next">
Next example: <a href="file-paths">File Paths</a>.
</p>
<p class="footer">
by <a href="https://markmcgranaghan.com">Mark McGranaghan</a> | <a href="https://github.com/mmcgrana/gobyexample/blob/master/examples/line-filters">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 \"bufio\"\u000A \"fmt\"\u000A \"os\"\u000A \"strings\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' scanner := bufio.NewScanner(os.Stdin)\u000A');codeLines.push(' for scanner.Scan() {\u000A');codeLines.push(' ucl := strings.ToUpper(scanner.Text())\u000A');codeLines.push(' fmt.Println(ucl)\u000A }\u000A');codeLines.push(' if err := scanner.Err(); err != nil {\u000A fmt.Fprintln(os.Stderr, \"error:\", err)\u000A os.Exit(1)\u000A }\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>
</html>