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 ```console
$ tool/build $ tool/build
$ open build/gobyexample.pdf $ open site/index.html
``` ```
### License ### 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 syntax is `func name(args) returns { body }`. Note
// the the types of arguments come after their name. // the the types of arguments come after their name.
// In this case we're taking a slice of floats and returning // In this case we're taking a slice of floats and
// a single float value. // returning a single float value.
func avg(vals []float64) float64 { func avg(vals []float64) float64 {
total := 0.0 total := 0.0
for _, val := range vals { for _, val := range vals {
total += val total += val
} }
// Go requires an expliciti return, i.e. it won't // 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)) return total / float64(len(vals))
} }
@ -30,6 +31,6 @@ func main() {
fmt.Println(output) fmt.Println(output)
} }
// There are several other features to Go functions, including // There are several other features to Go functions,
// multiple return values and varargs, which we'll look at // including multiple return values and varargs, which
// next. // 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 // There is no ternary operator (i.e. `?`) in Go, so
// need to use a full if/else even for very basic conditions. // 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 package main
import "strings" import s "strings"
import "fmt" import "fmt"
// Helper for all the printing we'll do in this example. // 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. // package, not methods on the string object itself.
// This means that we nned pass the string in question // This means that we nned pass the string in question
// as the first argument to the functions. // as the first argument to the functions.
p("Contains: ", strings.Contains("test", "es")) p("Contains: ", s.Contains("test", "es"))
p("Count: ", strings.Count("test", "t")) p("Count: ", s.Count("test", "t"))
p("HasPrefix: ", strings.HasPrefix("test", "te")) p("HasPrefix: ", s.HasPrefix("test", "te"))
p("HasSuffix: ", strings.HasSuffix("test", "st")) p("HasSuffix: ", s.HasSuffix("test", "st"))
p("Index: ", strings.Index("test", "e")) p("Index: ", s.Index("test", "e"))
p("Join: ", strings.Join([]string{"a", "b"}, "-")) p("Join: ", s.Join([]string{"a", "b"}, "-"))
p("Repeat: ", strings.Repeat("a", 5)) p("Repeat: ", s.Repeat("a", 5))
p("Replace: ", strings.Replace("foo", "o", "0", -1)) p("Replace: ", s.Replace("foo", "o", "0", -1))
p("Replace: ", strings.Replace("foo", "o", "0", 1)) p("Replace: ", s.Replace("foo", "o", "0", 1))
p("Split: ", strings.Split("a-b-c-d-e", "-")) p("Split: ", s.Split("a-b-c-d-e", "-"))
p("toLower: ", strings.ToLower("TEST")) p("toLower: ", s.ToLower("TEST"))
p("ToUpper: ", strings.ToUpper("test")) p("ToUpper: ", s.ToUpper("test"))
p() p()
// You can find more functions in the [`strings`]() // 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> <head>
<meta http-eqiv="content-type" content="text/html;charset=utf-8"> <meta http-eqiv="content-type" content="text/html;charset=utf-8">
<title>Go by Example</title> <title>Go by Example</title>
<link rel=stylesheet href="../src/book.css"> <link rel=stylesheet href="../src/style.css">
</head> </head>
<body>`) <body>`)

View File

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