move h2 out of source files

This commit is contained in:
Mark McGranaghan 2012-10-09 18:31:28 -07:00
parent 571a58ece4
commit bc39dbc769
89 changed files with 107 additions and 274 deletions

View File

@ -1,84 +1,84 @@
hello-world Hello World
# values # Values
# variables # Variables
# inline-assignment # Short Declarations
# constants # Constants
# for # For
# if-else # If/Else
# switch # Switch
# arrays # Arrays
# slices # Slices
# maps # Maps
# range # Range
# functions # Functions
# multiple-return-values # Multiple Return Values
# varadic-functions # Varadic Functions
# closures # Closures
# recursion # Recursion
# defer # Defer
# panic # Panic
# pointers # Pointers
# new # New
# structs # Structs
# methods # Methods
# embedding # Embedding
# interfaces # Interfaces
# errors # Errors
# ok-guards # OK Guards
# goroutines # Goroutines
# concurrent-goroutines # Concurrent Goroutines
# channels # Channels
# channel-buffering # Channel Buffering
# channel-directions # Channel Directions
# synchronization # Synchronization
# select # Select
# timeouts # Timeouts
# scatter-gather # Scatter Gather
# rate-limiting # Rate Limiting
# worker-pools # Worker Pools
# non-blocking-channel-operations # Non-Blocking Channel Operations
# closing-channels # Closing Channels
timers Timers
tickers Tickers
# state-goroutine # State Goroutine
# state-mutex # State Mutex
sorting Sorting
sorting-by-functions Sorting by Functions
# collection-functions # Collection Functions
# string-functions # String Functions
# string-formatting # String Formatting
# regexs # Regexs
# bytes # Bytes
# json # JSON
# time # Time
# epochs # Epochs
# elapsed-time # Elapsed Time
# random-numbers # Random Numbers
# number-parsing # Number Parsing
# urls # URLs
# sha1-hashes # SHA1 Hashes
# base64-encoding # Base64 Encoding
# reading-files # Reading Files
# writing-files # Writing Files
line-filters Line Filters
# command-line-arguments # Command-Line Arguments
# command-line-flags # Command-Line Flags
# environment-variables # Environment Variables
# spawning-processes # Spawning Processes
# execing-processes # Execing Processes
# signals # Signals
# exit # Exit
# http-client # HTTP Client
# https-client # HTTPS Client
# redis # Redis
# postgres # Postgres
# hello-web # Hello Web
# responses # Responses
# request-routing # Request Routing
# request-logging # Request Logging
# static-content # Static Content
# basic-authentication # Basic Authentication
# canonical-hosts # Canonical Hosts
# middleware # Middleware
# graceful-shutdown # Graceful Shutdown
# https-servers # HTTPS Servers

View File

@ -1,5 +1,3 @@
// ## Arrays
package main package main
import "fmt" import "fmt"

View File

@ -1,5 +1,3 @@
// ## Base64 Encoding
package main package main
import b64 "encoding/base64" import b64 "encoding/base64"

View File

@ -1,5 +1,3 @@
// ## Basic Authentication
package main package main
import ( import (

View File

@ -1,5 +1,3 @@
// ## Bytes
package main package main
import "fmt" import "fmt"

View File

@ -1,5 +1,3 @@
// ## Canonical Hosts
package main package main
import "net/http" import "net/http"

View File

@ -1,5 +1,3 @@
// ## Channel Buffering
package main package main
import "fmt" import "fmt"

View File

@ -1,5 +1,3 @@
// ## Channel Directions
package main package main
import "fmt" import "fmt"

View File

@ -1,5 +1,3 @@
// ## Channels
package main package main
import "fmt" import "fmt"

View File

@ -1,5 +1,3 @@
// ## Closing Channels
package main package main
import "fmt" import "fmt"

View File

@ -1,5 +1,3 @@
// ## Closures
package main package main
import "fmt" import "fmt"

View File

@ -1,5 +1,3 @@
// ## Collection Functions
package main package main
import "strings" import "strings"
@ -92,4 +90,4 @@ func main() {
fmt.Println() fmt.Println()
} }
// todo: generics // todo: note no generics

View File

@ -1,5 +1,3 @@
// ## Command Line Arguments
// Use `os.Args` to access command-line arguments and // Use `os.Args` to access command-line arguments and
// the name of the program. // the name of the program.
package main package main

View File

@ -1,5 +1,3 @@
// ## Command Line Flags
package main package main
import "flag" import "flag"

View File

@ -1,5 +1,3 @@
// ## Concurrent Goroutines
package main package main
import "time" import "time"

View File

@ -1,5 +1,3 @@
// ## Constants
package main package main
import "fmt" import "fmt"

View File

@ -1,5 +1,3 @@
// ## Defer
package main package main
import "fmt" import "fmt"

View File

@ -1,5 +1,3 @@
// ## Elapsed Time
package main package main
import "time" import "time"

View File

@ -1,5 +1,3 @@
// ## Embedding
package main package main
import "math" import "math"

View File

@ -1,5 +1,3 @@
// ## Environment Variables
package main package main
// Use the `os` package to list, set, and get environment // Use the `os` package to list, set, and get environment

View File

@ -1,5 +1,3 @@
// ## Epochs
// A common requirement in programms is getting the number // A common requirement in programms is getting the number
// of seconds, milliseconds, or nanoseconds since the Unix // of seconds, milliseconds, or nanoseconds since the Unix
// epoch. Here's how to do it in Go. // epoch. Here's how to do it in Go.

View File

@ -1,5 +1,3 @@
// ## Errors
package main package main
import ( import (

View File

@ -1,5 +1,3 @@
// ## Exec'ing Processes
// In the previous chapter we looked at spawning external // In the previous chapter we looked at spawning external
// process. We do this when we need the functionality // process. We do this when we need the functionality
// of another process accessable to a running Go process. // of another process accessable to a running Go process.

View File

@ -1,5 +1,3 @@
// ## Exit
// Use `os.Exit` to immediatly exit with a given // Use `os.Exit` to immediatly exit with a given
// status. // status.

View File

@ -1,5 +1,3 @@
// ## For
// `for` is Go's only looping construct. Here are // `for` is Go's only looping construct. Here are
// two common forms. // two common forms.
package main package main

View File

@ -1,5 +1,3 @@
// ## Functions
// Funcations are critical in Go as in any other language. // Funcations are critical in Go as in any other language.
// We'll look at some basic examples first. // We'll look at some basic examples first.

View File

@ -1,5 +1,3 @@
// ## Goroutines
package main package main
import "fmt" import "fmt"

View File

@ -1,5 +1,3 @@
// ## Graceful Shutdown
package main package main
import ( import (
@ -96,8 +94,5 @@ func main() {
} }
} }
// todo: clean up logging // todo: pull in work from gobyexample-server
// todo: limit shutdown time
// todo: factor out to cut-and-pastable against normal app // todo: factor out to cut-and-pastable against normal app
// todo: credit http://blog.nella.org/?p=879
// todo: comment about tcp servers

View File

@ -1,5 +1,3 @@
// ## Hello Web
package main package main
import "net/http" import "net/http"

View File

@ -1,5 +1,3 @@
// ## Hello World
// Our first program will print the classic "Hello world"` // Our first program will print the classic "Hello world"`
// message. Here's the full source code. // message. Here's the full source code.
package main package main

View File

@ -1,5 +1,3 @@
// ## HTTP Client
package main package main
import "net/http" import "net/http"

View File

@ -1,5 +1,3 @@
// ## HTTPS Client
package main package main
import "net/http" import "net/http"

View File

@ -1,5 +1,3 @@
// ## HTTPS Servers
package main package main
import "net/http" import "net/http"

View File

@ -1,6 +1,5 @@
// ## If/Else // If/else in Go is straight-forward.
// If/else in go is straight-forward
package main package main
import "fmt" import "fmt"

View File

@ -1,2 +0,0 @@
$ go run inline-assignment.go
Hello assignment

View File

@ -1,5 +1,3 @@
// ## Interfaces
package main package main
import "fmt" import "fmt"

View File

@ -1,5 +1,3 @@
// ## JSON
package main package main
import "encoding/json" import "encoding/json"

View File

@ -1,5 +1,3 @@
// ## Line Filters
// A _line filter_ is a common type of program that reads // A _line filter_ is a common type of program that reads
// input on stdin, processes it, and then prints some // input on stdin, processes it, and then prints some
// derived result to stdout. `grep` and `sed` are common // derived result to stdout. `grep` and `sed` are common

View File

@ -1,5 +1,3 @@
// ## Maps
// Maps are Go's built-in associative data type (sometimes // Maps are Go's built-in associative data type (sometimes
// called "hashes" or "dicts" in other languages). // called "hashes" or "dicts" in other languages).

View File

@ -1,5 +1,3 @@
// ## Methods
package main package main
import "fmt" import "fmt"

View File

@ -1,5 +1,3 @@
// ## Middleware
package main package main
import "net/http" import "net/http"

View File

@ -1,5 +1,3 @@
// ## Multiple Return Values
// Go has built-in support for multiple return values. // Go has built-in support for multiple return values.
// This feature is used often in idiomatic Go, for example // This feature is used often in idiomatic Go, for example
// to return both result and error values from a function. // to return both result and error values from a function.

View File

@ -1,5 +1,3 @@
// ## New
package main package main
import "fmt" import "fmt"

View File

@ -1,5 +1,3 @@
// ## Non-blocking Channel Operations
package main package main
import "fmt" import "fmt"

View File

@ -1,5 +1,3 @@
// ## Number Parsing
package main package main
// Package `strconv` provides the number parsing. // Package `strconv` provides the number parsing.

View File

@ -1,5 +1,3 @@
// ## OK Guards
package main package main
import "fmt" import "fmt"

View File

@ -1,5 +1,3 @@
// ## Panic
// A `panic` means something went unexpectedly wrong. // A `panic` means something went unexpectedly wrong.
// Mostly we use it to fail fast on errors that // Mostly we use it to fail fast on errors that
// shouldn't occur during normal operation. // shouldn't occur during normal operation.

View File

@ -1,5 +1,3 @@
// ## Pointers
package main package main
import "fmt" import "fmt"

View File

@ -1,5 +1,3 @@
// ## Postgres
package main package main
import _ "github.com/bmizerany/pq" import _ "github.com/bmizerany/pq"

View File

@ -1,5 +1,3 @@
// ## Random Numbers
package main package main
// The `math/rand` package provides psuedo-random // The `math/rand` package provides psuedo-random

View File

@ -1,5 +1,3 @@
// ## Range
package main package main
import "fmt" import "fmt"

View File

@ -1,4 +1,3 @@
// ## Rate Limiting
package main package main
import "time" import "time"

View File

@ -1,5 +1,3 @@
// ## Reading files
package main package main
import "io/ioutil" import "io/ioutil"

View File

@ -1,5 +1,3 @@
// ## Recursion
package main package main
import "fmt" import "fmt"

View File

@ -1,5 +1,3 @@
// ## Redis
package main package main
import "github.com/fzzbt/radix/redis" import "github.com/fzzbt/radix/redis"

View File

@ -1,5 +1,3 @@
// ## Regexs
package main package main
import "regexp" import "regexp"

View File

@ -1,5 +1,3 @@
// ## Request Logging
package main package main
import "net/http" import "net/http"

View File

@ -1,5 +1,3 @@
// ## HTTP Server Routing
package main package main
import "github.com/bmizerany/pat" import "github.com/bmizerany/pat"

View File

@ -1,5 +1,3 @@
// ## Responses
package main package main
import "net/http" import "net/http"

View File

@ -1,5 +1,3 @@
// ## Scatter-Gather
package main package main
import "sync" import "sync"

View File

@ -1,5 +1,3 @@
// ## Select
package main package main
import "time" import "time"

View File

@ -1,5 +1,3 @@
// ## SHA1 Hashes
package main package main
// Package `crypto/sha1` computes SHA1 hashes. // Package `crypto/sha1` computes SHA1 hashes.

View File

@ -1,11 +1,9 @@
// ## Inline Assignment
package main package main
import "fmt" import "fmt"
func main() { func main() {
// `x := val` is shorthand for `var x type = val`. // `x := val` is shorthand for `var x type = val`.
x := "Hello assignment" x := "Hello var"
fmt.Println(x) fmt.Println(x)
} }

View File

@ -0,0 +1,2 @@
$ go run short-declarations.go
Hello var

View File

@ -1,5 +1,3 @@
// ## Signals
// Sometines we'd like our Go programs to intelligently // Sometines we'd like our Go programs to intelligently
// handle Unix signals. For example, we might want a // handle Unix signals. For example, we might want a
// server to gracefully shutdown when it receives a // server to gracefully shutdown when it receives a

View File

@ -1,5 +1,3 @@
// ## Slices
package main package main
import "fmt" import "fmt"

View File

@ -1,5 +1,3 @@
// ## Sorting by Functions
// Sometimes we'll want to sort a collection by something // Sometimes we'll want to sort a collection by something
// other than its natural order. For example, suppose we // other than its natural order. For example, suppose we
// wanted to sort strings by their length instead of // wanted to sort strings by their length instead of

View File

@ -1,5 +1,3 @@
// ## Sorting
// Go's `sort` package implements sorting for builtins // Go's `sort` package implements sorting for builtins
// and user-defined types. We'll look at sorting for // and user-defined types. We'll look at sorting for
// builtins first. // builtins first.

View File

@ -1,5 +1,3 @@
// ## Spawning Processes
// Sometimes our Go programs need to spawn other, non-Go // Sometimes our Go programs need to spawn other, non-Go
// processes. For example, the syntax highlighting in this // processes. For example, the syntax highlighting in this
// book is implementing by spawning a [`pygmentize`]() // book is implementing by spawning a [`pygmentize`]()

View File

@ -1,5 +1,3 @@
// ## State Goroutine
package main package main
import "fmt" import "fmt"

View File

@ -1,5 +1,3 @@
// ## State Mutex
package main package main
import "fmt" import "fmt"
@ -64,3 +62,5 @@ func main() {
finalOpCount := atomic.LoadInt64(&opCount) finalOpCount := atomic.LoadInt64(&opCount)
fmt.Println(finalOpCount) fmt.Println(finalOpCount)
} }
// todo: "State with Mutexes?"

View File

@ -1,5 +1,3 @@
// ## Static Content
package main package main
import "net/http" import "net/http"

View File

@ -1,5 +1,3 @@
// ## String Formatting
package main package main
import "fmt" import "fmt"

View File

@ -1,5 +1,3 @@
// ## String Functions
// The standard library's `strings` package provides many // The standard library's `strings` package provides many
// useful string-related functions. // useful string-related functions.

View File

@ -1,5 +1,3 @@
// ## Structs
package main package main
import "fmt" import "fmt"

View File

@ -1,5 +1,3 @@
// ## Switch
// Switch statements allow... // Switch statements allow...
package main package main

View File

@ -1,5 +1,3 @@
// ## Synchronization
// We can use channels to synchronize execution // We can use channels to synchronize execution
// accross goroutines. Here's an example of waiting // accross goroutines. Here's an example of waiting
// for another goroutine to finish. // for another goroutine to finish.

View File

@ -1,5 +1,3 @@
// ## Tickers
// [Timers](timers) are for when you want to do // [Timers](timers) are for when you want to do
// something once in the future - tickers are for when you // something once in the future - tickers are for when you
// want to do something repeatedly at regular intervals. // want to do something repeatedly at regular intervals.

View File

@ -1,5 +1,3 @@
// ## Time
package main package main
import "time" import "time"

View File

@ -1,5 +1,3 @@
// ## Timeouts
package main package main
import "time" import "time"

View File

@ -1,5 +1,3 @@
// ## Timers
// We often want to execute Go code at some point in the // We often want to execute Go code at some point in the
// future, or repeatedly at some interval. Go's built-in // future, or repeatedly at some interval. Go's built-in
// timer and ticker features make both of these tasks // timer and ticker features make both of these tasks

View File

@ -1,5 +1,3 @@
// ## URLs
package main package main
import "fmt" import "fmt"

View File

@ -1,5 +1,3 @@
// ## Values
// Go has various value types, including strings, // Go has various value types, including strings,
// different types of numbers, booleans, etc. // different types of numbers, booleans, etc.
package main package main

View File

@ -1,5 +1,3 @@
// ## Varadic Functions
// Varadic functions can be called with any number of // Varadic functions can be called with any number of
// trailing arguments. This is useful if you don't know // trailing arguments. This is useful if you don't know
// number of arguments that will be needed for a function // number of arguments that will be needed for a function

View File

@ -1,5 +1,3 @@
// ## Variables
package main package main
import "fmt" import "fmt"

View File

@ -1,5 +1,3 @@
// ## Worker Pools
package main package main
import "time" import "time"

View File

@ -1,11 +1,9 @@
// ## Writing Files
package main package main
import "os" import "os"
func main() { func main() {
file, err := os.Create("xx-file-write.txt") file, err := os.Create("writing-files.txt")
if err != nil { if err != nil {
panic(err) panic(err)
} }

View File

@ -17,6 +17,7 @@
</script> </script>
<body> <body>
<div class="chapter" id="{{.Id}}"> <div class="chapter" id="{{.Id}}">
<h2>{{.Name}}</h2>
<table cellspacing="0" cellpadding="0"> <table cellspacing="0" cellpadding="0">
<tbody> <tbody>
{{range .Segs}} {{range .Segs}}

View File

@ -130,7 +130,6 @@ type Chapter struct {
func parseSegs(sourcePath string) []*Seg { func parseSegs(sourcePath string) []*Seg {
lines := readLines(sourcePath) lines := readLines(sourcePath)
segs := []*Seg{} segs := []*Seg{}
segs = append(segs, &Seg{Code: "", Docs: ""})
lastSeen := "" lastSeen := ""
for _, line := range lines { for _, line := range lines {
if line == "" { if line == "" {
@ -142,9 +141,8 @@ func parseSegs(sourcePath string) []*Seg {
} }
matchDocs := docsPat.MatchString(line) matchDocs := docsPat.MatchString(line)
matchCode := !matchDocs matchCode := !matchDocs
lastSeg := segs[len(segs)-1] newDocs := (lastSeen == "") || ((lastSeen != "docs") && (segs[len(segs)-1].Docs != ""))
newDocs := (lastSeen == "") || ((lastSeen != "docs") && (lastSeg.Docs != "")) newCode := (lastSeen == "") || ((lastSeen != "code") && (segs[len(segs)-1].Code != ""))
newCode := (lastSeen == "") || ((lastSeen != "code") && (lastSeg.Code != ""))
if newDocs || newCode { if newDocs || newCode {
debug("NEWSEG") debug("NEWSEG")
} }
@ -154,7 +152,7 @@ func parseSegs(sourcePath string) []*Seg {
newSeg := Seg{Docs: trimmed, Code: ""} newSeg := Seg{Docs: trimmed, Code: ""}
segs = append(segs, &newSeg) segs = append(segs, &newSeg)
} else { } else {
lastSeg.Docs = lastSeg.Docs + "\n" + trimmed segs[len(segs)-1].Docs = segs[len(segs)-1].Docs + "\n" + trimmed
} }
debug("DOCS: " + line) debug("DOCS: " + line)
lastSeen = "docs" lastSeen = "docs"
@ -163,7 +161,7 @@ func parseSegs(sourcePath string) []*Seg {
newSeg := Seg{Docs: "", Code: line} newSeg := Seg{Docs: "", Code: line}
segs = append(segs, &newSeg) segs = append(segs, &newSeg)
} else { } else {
lastSeg.Code = lastSeg.Code + "\n" + line segs[len(segs)-1].Code = segs[len(segs)-1].Code + "\n" + line
} }
debug("CODE: " + line) debug("CODE: " + line)
lastSeen = "code" lastSeen = "code"
@ -187,15 +185,16 @@ func parseAndRenderSegs(sourcePath string) []*Seg {
} }
func parseChapters() []*Chapter { func parseChapters() []*Chapter {
chapterLines := readLines("meta/chapters.txt") chapterNames := readLines("meta/chapters.txt")
chapters := make([]*Chapter, 0) chapters := make([]*Chapter, 0)
for _, chapterId := range chapterLines { for _, chapterName := range chapterNames {
if (chapterId != "") && !strings.HasPrefix(chapterId, "#") { if (chapterName != "") && !strings.HasPrefix(chapterName, "#") {
chapter := Chapter{Id: chapterId} chapter := Chapter{Name: chapterName}
chapterLines := readLines("src/" + chapterId + "/" + chapterId + ".go") chapterId := strings.ToLower(chapterName)
chapter.Name = chapterLines[0][6:] chapterId = strings.Replace(chapterId, " ", "-", -1)
chapterPath := "src/" + chapterId chapterId = strings.Replace(chapterId, "/", "-", -1)
sourcePaths := mustGlob(chapterPath + "/*") chapter.Id = chapterId
sourcePaths := mustGlob("src/" + chapterId + "/*")
segs := []*Seg{} segs := []*Seg{}
for _, sourcePath := range sourcePaths { for _, sourcePath := range sourcePaths {
sourceSegs := parseAndRenderSegs(sourcePath) sourceSegs := parseAndRenderSegs(sourcePath)