This commit is contained in:
Mark McGranaghan 2012-10-09 07:41:41 -07:00
parent 01e071b533
commit b78f66e305
14 changed files with 29 additions and 31 deletions

View File

@ -6,7 +6,7 @@ Go by Example book source and build toolchain.
```console
$ tool/build
$ open build/gobyexample.pdf
$ open site/index.html
```
### License

1
src/exit/.gitignore vendored
View File

@ -1 +0,0 @@
exit

View File

@ -9,15 +9,16 @@ import "fmt"
// The syntax is `func name(args) returns { body }`. Note
// the the types of arguments come after their name.
// In this case we're taking a slice of floats and returning
// a single float value.
// In this case we're taking a slice of floats and
// returning a single float value.
func avg(vals []float64) float64 {
total := 0.0
for _, val := range vals {
total += val
}
// Go requires an expliciti return, i.e. it won't
// automatically return the value of the last expression.
// automatically return the value of the last
// expression.
return total / float64(len(vals))
}
@ -30,6 +31,6 @@ func main() {
fmt.Println(output)
}
// There are several other features to Go functions, including
// multiple return values and varargs, which we'll look at
// next.
// There are several other features to Go functions,
// including multiple return values and varargs, which
// we'll look at next.

Binary file not shown.

View File

@ -19,5 +19,6 @@ func main() {
}
}
// There is no ternary operator (i.e. `?`) in Go, so you'll
// need to use a full if/else even for very basic conditions.
// There is no ternary operator (i.e. `?`) in Go, so
// you'll need to use a full if/else even for very basic
// conditions.

Binary file not shown.

Binary file not shown.

View File

@ -5,7 +5,7 @@
package main
import "strings"
import s "strings"
import "fmt"
// Helper for all the printing we'll do in this example.
@ -19,18 +19,18 @@ func main() {
// package, not methods on the string object itself.
// This means that we nned pass the string in question
// as the first argument to the functions.
p("Contains: ", strings.Contains("test", "es"))
p("Count: ", strings.Count("test", "t"))
p("HasPrefix: ", strings.HasPrefix("test", "te"))
p("HasSuffix: ", strings.HasSuffix("test", "st"))
p("Index: ", strings.Index("test", "e"))
p("Join: ", strings.Join([]string{"a", "b"}, "-"))
p("Repeat: ", strings.Repeat("a", 5))
p("Replace: ", strings.Replace("foo", "o", "0", -1))
p("Replace: ", strings.Replace("foo", "o", "0", 1))
p("Split: ", strings.Split("a-b-c-d-e", "-"))
p("toLower: ", strings.ToLower("TEST"))
p("ToUpper: ", strings.ToUpper("test"))
p("Contains: ", s.Contains("test", "es"))
p("Count: ", s.Count("test", "t"))
p("HasPrefix: ", s.HasPrefix("test", "te"))
p("HasSuffix: ", s.HasSuffix("test", "st"))
p("Index: ", s.Index("test", "e"))
p("Join: ", s.Join([]string{"a", "b"}, "-"))
p("Repeat: ", s.Repeat("a", 5))
p("Replace: ", s.Replace("foo", "o", "0", -1))
p("Replace: ", s.Replace("foo", "o", "0", 1))
p("Split: ", s.Split("a-b-c-d-e", "-"))
p("toLower: ", s.ToLower("TEST"))
p("ToUpper: ", s.ToUpper("test"))
p()
// You can find more functions in the [`strings`]()

View File

@ -1,3 +0,0 @@
#!/bin/bash
exec go run tool/build-html.go $1

View File

@ -1,3 +0,0 @@
#!/bin/bash
exec prince $1 -o $2

3
tool/generate Executable file
View File

@ -0,0 +1,3 @@
#!/bin/bash
exec go run tool/generate.go

View File

@ -205,7 +205,7 @@ func main() {
<head>
<meta http-eqiv="content-type" content="text/html;charset=utf-8">
<title>Go by Example</title>
<link rel=stylesheet href="../src/book.css">
<link rel=stylesheet href="../src/style.css">
</head>
<body>`)

View File

@ -24,7 +24,7 @@ func readLines(path string) []string {
var todoPat = regexp.MustCompile("\\/\\/ todo: ")
func main() {
sourcePaths, err := filepath.Glob("./src/0*/*")
sourcePaths, err := filepath.Glob("./src/*/*")
check(err)
foundLongFile := false
for _, sourcePath := range sourcePaths {