update book code

This commit is contained in:
Unknwon
2015-03-03 12:25:25 -05:00
parent b8c82ba4e5
commit eab1d98ba8
465 changed files with 15392 additions and 1572 deletions

View File

@@ -0,0 +1,152 @@
<!DOCTYPE html>
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Package sort - The Go Programming Language</title>
<link rel="stylesheet" href="Package%20sort%20-%20The%20Go%20Programming%20Language_files/all.css" type="text/css" media="all" charset="utf-8">
<!--[if lt IE 8]>
<link rel="stylesheet" href="/doc/ie.css" type="text/css">
<![endif]-->
<script type="text/javascript" src="Package%20sort%20-%20The%20Go%20Programming%20Language_files/godocs.js"></script>
</head>
<body>
<div id="container">
<div id="topnav">
<h1 id="title">The Go Programming Language</h1>
<div id="nav-main">
<ul>
<li><a href="http://localhost:6060/">Home</a></li><li><a href="http://localhost:6060/doc/install.html">Getting Started</a></li><li><a href="http://localhost:6060/doc/docs.html">Documentation</a></li><li><a href="http://localhost:6060/doc/contrib.html">Contributing</a></li><li><a href="http://localhost:6060/doc/community.html">Community</a></li>
</ul>
<div class="quickref">
<form method="GET" action="/search">
<a href="http://localhost:6060/pkg/doc_example">doc_example</a> <span class="sep">|</span>
<a href="http://localhost:6060/pkg/">Packages</a> <span class="sep">|</span>
<a href="http://localhost:6060/cmd/">Commands</a> <span class="sep">|</span>
<a href="http://localhost:6060/doc/go_spec.html">Specification</a>
</form>
</div>
</div>
<a id="logo-box" href="http://localhost:6060/"></a>
</div>
<div id="content">
<!-- Menu is HTML-escaped elsewhere -->
<h1 id="generatedHeader">Package sort</h1>
<!-- The Table of Contents is automatically inserted in this <div>.
Do not delete this <div>. -->
<div id="nav"></div>
<!-- Content is HTML-escaped elsewhere -->
<!--
Copyright 2009 The Go Authors. All rights reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file.
-->
<!-- PackageName is printed as title by the top-level template -->
<p><code>import "doc_example"</code></p>
<p>
Sorting using a general interface:
</p>
<p>
</p><h4>Package files</h4>
<span style="font-size:90%">
<a href="http://localhost:6060/doc_example/sort.go">sort.go</a>
</span>
<p></p>
<h2 id="Float64sAreSorted">func <a href="http://localhost:6060/doc_example/sort.go?s=1687:1727#L48">Float64sAreSorted</a></h2>
<p><code>func Float64sAreSorted(a []float64) bool</code></p>
<h2 id="IntsAreSorted">func <a href="http://localhost:6060/doc_example/sort.go?s=1612:1644#L47">IntsAreSorted</a></h2>
<p><code>func IntsAreSorted(a []int) bool</code></p>
<h2 id="IsSorted">func <a href="http://localhost:6060/doc_example/sort.go?s=528:562#L15">IsSorted</a></h2>
<p><code>func IsSorted(data Interface) bool</code></p>
<p>
Test if data is sorted
</p>
<h2 id="Sort">func <a href="http://localhost:6060/doc_example/sort.go?s=351:376#L6">Sort</a></h2>
<p><code>func Sort(data Interface)</code></p>
<p>
General sort function
</p>
<h2 id="SortFloat64s">func <a href="http://localhost:6060/doc_example/sort.go?s=1495:1525#L44">SortFloat64s</a></h2>
<p><code>func SortFloat64s(a []float64)</code></p>
<h2 id="SortInts">func <a href="http://localhost:6060/doc_example/sort.go?s=1441:1463#L43">SortInts</a></h2>
<p><code>func SortInts(a []int)</code></p>
<p>
Convenience wrappers for common cases
</p>
<h2 id="SortStrings">func <a href="http://localhost:6060/doc_example/sort.go?s=1553:1581#L45">SortStrings</a></h2>
<p><code>func SortStrings(a []string)</code></p>
<h2 id="StringsAreSorted">func <a href="http://localhost:6060/doc_example/sort.go?s=1766:1804#L49">StringsAreSorted</a></h2>
<p><code>func StringsAreSorted(a []string) bool</code></p>
<h2 id="Float64Array">type <a href="http://localhost:6060/doc_example/sort.go?s=946:973#L32">Float64Array</a></h2>
<p></p><pre>type Float64Array []float64</pre><p></p>
<h3 id="Float64Array.Len">func (Float64Array) <a href="http://localhost:6060/doc_example/sort.go?s=975:1006#L33">Len</a></h3>
<p><code>func (p Float64Array) Len() int</code></p>
<h3 id="Float64Array.Less">func (Float64Array) <a href="http://localhost:6060/doc_example/sort.go?s=1036:1077#L34">Less</a></h3>
<p><code>func (p Float64Array) Less(i, j int) bool</code></p>
<h3 id="Float64Array.Swap">func (Float64Array) <a href="http://localhost:6060/doc_example/sort.go?s=1102:1138#L35">Swap</a></h3>
<p><code>func (p Float64Array) Swap(i, j int)</code></p>
<h2 id="IntArray">type <a href="http://localhost:6060/doc_example/sort.go?s=735:754#L26">IntArray</a></h2>
<p>
Convenience types for common cases: IntArray
</p>
<p></p><pre>type IntArray []int</pre><p></p>
<h3 id="IntArray.Len">func (IntArray) <a href="http://localhost:6060/doc_example/sort.go?s=758:785#L28">Len</a></h3>
<p><code>func (p IntArray) Len() int</code></p>
<h3 id="IntArray.Less">func (IntArray) <a href="http://localhost:6060/doc_example/sort.go?s=815:852#L29">Less</a></h3>
<p><code>func (p IntArray) Less(i, j int) bool</code></p>
<h3 id="IntArray.Swap">func (IntArray) <a href="http://localhost:6060/doc_example/sort.go?s=877:909#L30">Swap</a></h3>
<p><code>func (p IntArray) Swap(i, j int)</code></p>
<h2 id="Interface">type <a href="http://localhost:6060/doc_example/sort.go?s=241:321#L1">Interface</a></h2>
<p>
Sorting interface
</p>
<p></p><pre>type Interface interface {
Len() int
Less(i, j int) bool
Swap(i, j int)
}</pre><p></p>
<h2 id="StringArray">type <a href="http://localhost:6060/doc_example/sort.go?s=1175:1200#L37">StringArray</a></h2>
<p></p><pre>type StringArray []string</pre><p></p>
<h3 id="StringArray.Len">func (StringArray) <a href="http://localhost:6060/doc_example/sort.go?s=1202:1232#L38">Len</a></h3>
<p><code>func (p StringArray) Len() int</code></p>
<h3 id="StringArray.Less">func (StringArray) <a href="http://localhost:6060/doc_example/sort.go?s=1262:1302#L39">Less</a></h3>
<p><code>func (p StringArray) Less(i, j int) bool</code></p>
<h3 id="StringArray.Swap">func (StringArray) <a href="http://localhost:6060/doc_example/sort.go?s=1327:1362#L40">Swap</a></h3>
<p><code>func (p StringArray) Swap(i, j int)</code></p>
<h2>Other packages</h2>
<p>
<a href="http://localhost:6060/pkg/doc_example/?p=main">main</a><br>
</p>
</div>
<div id="site-info">
<p>Build version release.r58 8731. Except as noted, this content is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by/3.0/">Creative Commons Attribution 3.0 License</a>.</p>
</div>
</div>
</body></html>

View File

@@ -0,0 +1,205 @@
/* General Styles */
body {
font-family: "Bitstream Vera Sans", Verdana, sans-serif;
font-size: 81.25%;
line-height: 1.23em;
padding: 0;
margin: 1.23em;
background: white;
color: black;
}
a {
color: #04a;
text-decoration: none;
}
a:visited {
color: #04a;
}
a:hover {
color: #a40;
text-decoration: underline;
}
a:active {
color: #c00;
}
code, pre {
font-size: 1.2em;
}
pre {
background: #F0F0F0;
padding: 0.5em 1em;
}
/* Top bar */
#container {
width: 100%;
margin: auto;
}
#topnav {
height: 55px;
background: url(/doc/logo.png) no-repeat top left;
}
a#logo-box {
display: block;
height: 55px;
}
h1#title {
display: none;
}
#nav-main {
float: right;
width: 500px;
margin-top: -5px;
text-align: center;
}
#nav-main ul {
padding-left: 0;
margin-left: 0;
margin-bottom: 0.5em;
}
#nav-main li a {
display: inline;
display: inline-block;
padding: .46em .62em .38em .62em;
}
#nav-main li a:link,
#nav-main li a:visited {
color: #000;
}
#nav-main li {
display: inline;
display: inline-block;
background: #e6e6e6 url(/doc/button_background.png) repeat-x;
border: solid 1px #999;
margin-left: -1px;
text-shadow: #fff 0 1px 0;
box-shadow: 0 1px 1px #ccc;
-moz-box-shadow: 0 1px 1px #ccc;
-webkit-box-shadow: 0 1px 1px #ccc;
}
#nav-main li:first-child {
-moz-border-top-left-radius: 4px;
border-top-left-radius: 4px;
-moz-border-bottom-left-radius: 4px;
border-bottom-left-radius: 4px;
}
#nav-main li:last-child {
-moz-border-top-right-radius: 4px;
border-top-right-radius: 4px;
-moz-border-bottom-right-radius: 4px;
border-bottom-right-radius: 4px;
}
#nav-main .quickref {
color: #444;
}
#nav-main .quickref .sep {
color: #999;
}
#search {
width: 120px;
margin-left: 0.5em;
}
#search.inactive {
text-align: center;
color: #444;
}
/* Footer */
#site-info {
position: relative;
text-align: center;
}
#site-info, #site-info a:link, #site-info a:visited {
color: #aaa;
}
/* Content */
#content {
clear: both;
padding: 0;
position: relative;
margin-top: 1.5em;
margin-bottom: 1.5em;
border-top: solid 1px #aaa;
border-bottom: solid 1px #aaa;
}
.left-column {
width: 49%;
float: left;
}
.right-column {
width: 49%;
float: right;
}
.end-columns {
clear: both;
}
#content h1 {
margin-bottom: -0em;
padding: 0;
}
#content h2 {
border-top: 2px solid #ddd;
padding: 8px 0;
margin: 1.5em 0 0;
}
#content .subtitle {
margin-top: 1em;
display: block;
}
.navtop a {
font-weight: normal; font-size: 7pt;
float: right; color: #999;
}
/* Content and Code Highlighting */
pre.ebnf, pre.grammar {
background: #FFFFE0;
}
span.ln {
font-size: 80%;
color: #777777;
}
span.comment {
color: #002090;
}
span.highlight {
background: #FF9900;
font-weight: bold;
}
span.highlight-comment {
background: #FF9900;
font-weight: bold;
color: #002090;
}
span.selection {
background: #FFFF00
}
span.selection-comment {
color: #002090;
background: #FFFF00
}
span.selection-highlight {
background: #FF9900;
font-weight: bold;
}
span.selection-highlight-comment {
background: #FF9900;
font-weight: bold;
color: #002090;
}
span.alert {
color: #D00000;
}
#nav table {
width: 100%;
}
.detail {
padding: 0.25em 1em;
background: #F4F4F4;
}
sup.new {
color: red;
font-size: 8px;
line-height: 0;
}

View File

@@ -0,0 +1,190 @@
// Except as noted, this content is licensed under Creative Commons
// Attribution 3.0
/* A little code to ease navigation of these documents.
*
* On window load we:
* + Generate a table of contents (godocs_generateTOC)
* + Add links up to the top of the doc from each section (godocs_addTopLinks)
*/
/* We want to do some stuff on page load (after the HTML is rendered).
So listen for that:
*/
function bindEvent(el, e, fn) {
if (el.addEventListener){
el.addEventListener(e, fn, false);
} else if (el.attachEvent){
el.attachEvent('on'+e, fn);
}
}
bindEvent(window, 'load', godocs_onload);
function godocs_onload() {
godocs_bindSearchEvents();
godocs_generateTOC();
godocs_addTopLinks();
}
function godocs_bindSearchEvents() {
var search = document.getElementById('search');
if (!search) {
// no search box (index disabled)
return;
}
function clearInactive() {
if (search.className == "inactive") {
search.value = "";
search.className = "";
}
}
function restoreInactive() {
if (search.value != "") {
return;
}
if (search.type != "search") {
search.value = search.getAttribute("placeholder");
}
search.className = "inactive";
}
restoreInactive();
bindEvent(search, 'focus', clearInactive);
bindEvent(search, 'blur', restoreInactive);
}
/* Generates a table of contents: looks for h2 and h3 elements and generates
* links. "Decorates" the element with id=="nav" with this table of contents.
*/
function godocs_generateTOC() {
var navbar = document.getElementById('nav');
if (!navbar) { return; }
var toc_items = [];
var i;
for (i = 0; i < navbar.parentNode.childNodes.length; i++) {
var node = navbar.parentNode.childNodes[i];
if ((node.tagName == 'h2') || (node.tagName == 'H2')) {
if (!node.id) {
node.id = 'tmp_' + i;
}
var text = godocs_nodeToText(node);
if (!text) { continue; }
var textNode = document.createTextNode(text);
var link = document.createElement('a');
link.href = '#' + node.id;
link.appendChild(textNode);
// Then create the item itself
var item = document.createElement('dt');
item.appendChild(link);
toc_items.push(item);
}
if ((node.tagName == 'h3') || (node.tagName == 'H3')) {
if (!node.id) {
node.id = 'tmp_' + i;
}
var text = godocs_nodeToText(node);
if (!text) { continue; }
var textNode = document.createTextNode(text);
var link = document.createElement('a');
link.href = '#' + node.id;
link.appendChild(textNode);
// Then create the item itself
var item = document.createElement('dd');
item.appendChild(link);
toc_items.push(item);
}
}
if (toc_items.length <= 1) { return; }
var dl1 = document.createElement('dl');
var dl2 = document.createElement('dl');
var split_index = (toc_items.length / 2) + 1;
if (split_index < 8) {
split_index = toc_items.length;
}
for (i = 0; i < split_index; i++) {
dl1.appendChild(toc_items[i]);
}
for (/* keep using i */; i < toc_items.length; i++) {
dl2.appendChild(toc_items[i]);
}
var tocTable = document.createElement('table');
navbar.appendChild(tocTable);
tocTable.className = 'unruled';
var tocBody = document.createElement('tbody');
tocTable.appendChild(tocBody);
var tocRow = document.createElement('tr');
tocBody.appendChild(tocRow);
// 1st column
var tocCell = document.createElement('td');
tocCell.className = 'first';
tocRow.appendChild(tocCell);
tocCell.appendChild(dl1);
// 2nd column
tocCell = document.createElement('td');
tocRow.appendChild(tocCell);
tocCell.appendChild(dl2);
}
/* Returns the "This sweet header" from <h2>This <i>sweet</i> header</h2>.
* Takes a node, returns a string.
*/
function godocs_nodeToText(node) {
var TEXT_NODE = 3; // Defined in Mozilla but not MSIE :(
var text = '';
for (var j = 0; j != node.childNodes.length; j++) {
var child = node.childNodes[j];
if (child.nodeType == TEXT_NODE) {
if (child.nodeValue != '[Top]') { //ok, that's a hack, but it works.
text = text + child.nodeValue;
}
} else {
text = text + godocs_nodeToText(child);
}
}
return text;
}
/* For each H2 heading, add a link up to the #top of the document.
* (As part of this: ensure existence of 'top' named anchor link
* (theoretically at doc's top).)
*/
function godocs_addTopLinks() {
/* Make sure there's a "top" to link to. */
var top = document.getElementById('top');
if (!top) {
document.body.id = 'top';
}
if (!document.getElementsByTagName) return; // no browser support
var headers = document.getElementsByTagName('h2');
for (var i = 0; i < headers.length; i++) {
var span = document.createElement('span');
span.className = 'navtop';
var link = document.createElement('a');
span.appendChild(link);
link.href = '#top';
var textNode = document.createTextNode('[Top]');
link.appendChild(textNode);
headers[i].appendChild(span);
}
}

View File

@@ -0,0 +1,59 @@
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Sorting using a general interface
package sort
// Sorting interface
type Interface interface {
Len() int
Less(i, j int) bool
Swap(i, j int)
}
// General sort function
func Sort(data Interface) {
for i := 1; i < data.Len(); i++ {
for j := i; j > 0 && data.Less(j, j-1); j-- {
data.Swap(j, j-1)
}
}
}
// Test if data is sorted
func IsSorted(data Interface) bool {
n := data.Len()
for i := n - 1; i > 0; i-- {
if data.Less(i, i-1) {
return false
}
}
return true
}
// Convenience types for common cases: IntArray
type IntArray []int
func (p IntArray) Len() int { return len(p) }
func (p IntArray) Less(i, j int) bool { return p[i] < p[j] }
func (p IntArray) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
type Float64Array []float64
func (p Float64Array) Len() int { return len(p) }
func (p Float64Array) Less(i, j int) bool { return p[i] < p[j] }
func (p Float64Array) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
type StringArray []string
func (p StringArray) Len() int { return len(p) }
func (p StringArray) Less(i, j int) bool { return p[i] < p[j] }
func (p StringArray) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
// Convenience wrappers for common cases
func SortInts(a []int) { Sort(IntArray(a)) }
func SortFloat64s(a []float64) { Sort(Float64Array(a)) }
func SortStrings(a []string) { Sort(StringArray(a)) }
func IntsAreSorted(a []int) bool { return IsSorted(IntArray(a)) }
func Float64sAreSorted(a []float64) bool { return IsSorted(Float64Array(a)) }
func StringsAreSorted(a []string) bool { return IsSorted(StringArray(a)) }

View File

@@ -0,0 +1,82 @@
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// This package gives an example of how to use a custom package with interfaces
package main
import (
"fmt"
"./sort"
)
// sorting of slice of integers
func ints() {
data := []int{74, 59, 238, -784, 9845, 959, 905, 0, 0, 42, 7586, -5467984, 7586}
a := sort.IntArray(data) //conversion to type IntArray
sort.Sort(a)
if !sort.IsSorted(a) {
panic("fail")
}
fmt.Printf("The sorted array is: %v\n", a)
}
// sorting of slice of strings
func strings() {
data := []string{"monday", "friday", "tuesday", "wednesday", "sunday","thursday", "", "saturday"}
a := sort.StringArray(data)
sort.Sort(a)
if !sort.IsSorted(a) {
panic("fail")
}
fmt.Printf("The sorted array is: %v\n", a)
}
// a type which describes a day of the week
type day struct {
num int
shortName string
longName string
}
type dayArray struct {
data []*day
}
func (p *dayArray) Len() int { return len(p.data) }
func (p *dayArray) Less(i, j int) bool { return p.data[i].num < p.data[j].num }
func (p *dayArray) Swap(i, j int) { p.data[i], p.data[j] = p.data[j], p.data[i] }
// sorting of custom type day
func days() {
Sunday := day{0, "SUN", "Sunday"}
Monday := day{1, "MON", "Monday"}
Tuesday := day{2, "TUE", "Tuesday"}
Wednesday := day{3, "WED", "Wednesday"}
Thursday := day{4, "THU", "Thursday"}
Friday := day{5, "FRI", "Friday"}
Saturday := day{6, "SAT", "Saturday"}
data := []*day{&Tuesday, &Thursday, &Wednesday, &Sunday, &Monday, &Friday, &Saturday}
a := dayArray{data}
sort.Sort(&a)
if !sort.IsSorted(&a) {
panic("fail")
}
for _, d := range data {
fmt.Printf("%s ", d.longName)
}
fmt.Printf("\n")
}
func main() {
ints()
strings()
days()
}
/* Output:
The sorted array is: [-5467984 -784 0 0 42 59 74 238 905 959 7586 7586 9845]
The sorted array is: [ friday monday saturday sunday thursday tuesday wednesday]
Sunday Monday Tuesday Wednesday Thursday Friday Saturday
*/