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

168 lines
6.4 KiB
Plaintext
Generated

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Go by Example: Sorting</title>
<link rel=stylesheet href="site.css">
</head>
<script>
onkeydown = (e) => {
if (e.key == "ArrowLeft") {
window.location.href = 'stateful-goroutines';
}
if (e.key == "ArrowRight") {
window.location.href = 'sorting-by-functions';
}
}
</script>
<body>
<div class="example" id="sorting">
<h2><a href="./">Go by Example</a>: Sorting</h2>
<table>
<tr>
<td class="docs">
<p>Go&rsquo;s <code>sort</code> package implements sorting for builtins
and user-defined types. We&rsquo;ll look at sorting for
builtins first.</p>
</td>
<td class="code empty leading">
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<a href="http://play.golang.org/p/_gY0tANzJ4l"><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;sort&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>Sort methods are specific to the builtin type;
here&rsquo;s an example for strings. Note that sorting is
in-place, so it changes the given slice and doesn&rsquo;t
return a new one.</p>
</td>
<td class="code leading">
<div class="highlight"><pre> <span class="nx">strs</span> <span class="o">:=</span> <span class="p">[]</span><span class="kt">string</span><span class="p">{</span><span class="s">&quot;c&quot;</span><span class="p">,</span> <span class="s">&quot;a&quot;</span><span class="p">,</span> <span class="s">&quot;b&quot;</span><span class="p">}</span>
<span class="nx">sort</span><span class="p">.</span><span class="nx">Strings</span><span class="p">(</span><span class="nx">strs</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;Strings:&quot;</span><span class="p">,</span> <span class="nx">strs</span><span class="p">)</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
<p>An example of sorting <code>int</code>s.</p>
</td>
<td class="code leading">
<div class="highlight"><pre> <span class="nx">ints</span> <span class="o">:=</span> <span class="p">[]</span><span class="kt">int</span><span class="p">{</span><span class="mi">7</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">4</span><span class="p">}</span>
<span class="nx">sort</span><span class="p">.</span><span class="nx">Ints</span><span class="p">(</span><span class="nx">ints</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;Ints: &quot;</span><span class="p">,</span> <span class="nx">ints</span><span class="p">)</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
<p>We can also use <code>sort</code> to check if a slice is
already in sorted order.</p>
</td>
<td class="code">
<div class="highlight"><pre> <span class="nx">s</span> <span class="o">:=</span> <span class="nx">sort</span><span class="p">.</span><span class="nx">IntsAreSorted</span><span class="p">(</span><span class="nx">ints</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;Sorted: &quot;</span><span class="p">,</span> <span class="nx">s</span><span class="p">)</span>
<span class="p">}</span>
</pre></div>
</td>
</tr>
</table>
<table>
<tr>
<td class="docs">
<p>Running our program prints the sorted string and int
slices and <code>true</code> as the result of our <code>AreSorted</code> test.</p>
</td>
<td class="code">
<div class="highlight"><pre><span class="gp">$</span> go run sorting.go
<span class="go">Strings: [a b c]</span>
<span class="go">Ints: [2 4 7]</span>
<span class="go">Sorted: true</span>
</pre></div>
</td>
</tr>
</table>
<p class="next">
Next example: <a href="sorting-by-functions">Sorting by Functions</a>.
</p>
<p class="footer">
by <a href="https://markmcgranaghan.com">Mark McGranaghan</a> | <a href="https://github.com/mmcgrana/gobyexample/blob/master/examples/sorting">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 \"fmt\"\u000A \"sort\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' strs :\u003D []string{\"c\", \"a\", \"b\"}\u000A sort.Strings(strs)\u000A fmt.Println(\"Strings:\", strs)\u000A');codeLines.push(' ints :\u003D []int{7, 2, 4}\u000A sort.Ints(ints)\u000A fmt.Println(\"Ints: \", ints)\u000A');codeLines.push(' s :\u003D sort.IntsAreSorted(ints)\u000A fmt.Println(\"Sorted: \", s)\u000A}\u000A');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>
</html>