diff --git a/src/037-rate-limiting/rate-limiting.go b/src/037-rate-limiting/rate-limiting.go index 5ffd5ab..4f7a71c 100644 --- a/src/037-rate-limiting/rate-limiting.go +++ b/src/037-rate-limiting/rate-limiting.go @@ -12,4 +12,4 @@ func main() { } } -// todo: credit http://code.google.com/p/go-wiki/wiki/RateLimiting +// todo: credit code.google.com/p/go-wiki/wiki/RateLimiting diff --git a/src/056-random-numbers/random-numbers.go b/src/056-random-numbers/random-numbers.go index 2e91d78..4b09b6c 100644 --- a/src/056-random-numbers/random-numbers.go +++ b/src/056-random-numbers/random-numbers.go @@ -19,7 +19,7 @@ func main() { fmt.Println(rand.Float64()) // To make the psuedo-random generator deterministic, - // give it a well-known seed. + // give it a well-known seed. s1 := rand.NewSource(42) r1 := rand.New(s1) diff --git a/src/059-sha1-hashes/sha1-hashes.go b/src/059-sha1-hashes/sha1-hashes.go index d451c41..85a60cd 100644 --- a/src/059-sha1-hashes/sha1-hashes.go +++ b/src/059-sha1-hashes/sha1-hashes.go @@ -1,27 +1,27 @@ -// ## SHA1 Hashes +// ## SHA1 Hashes package main -// Package `crypto/sha1` computes SHA1 hashes. +// Package `crypto/sha1` computes SHA1 hashes. import "crypto/sha1" import "encoding/hex" import "fmt" func main() { - // The pattern is `sha1.New()`, `sha1.Write(bytes)`, + // The pattern is `sha1.New()`, `sha1.Write(bytes)`, // then `sha1.Sum([]byte{}) h := sha1.New() - // `Write` expects bytes. If you have a string `s` - // use `[]byte(s)` to coerce it. + // `Write` expects bytes. If you have a string `s` + // use `[]byte(s)` to coerce it. h.Write([]byte("sha1 this string")) // Get the result. The argument to `Sum` can be used - // to append to an existing buffer: usually uneeded. + // to append to an existing buffer: usually uneeded. bs := h.Sum(nil) - // SHA1 values are often printed in hex, for example - // with git. + // SHA1 values are often printed in hex, for example + // with git. fmt.Println(hex.EncodeToString(bs)) } diff --git a/src/066-environment-variables/environment-variables.go b/src/066-environment-variables/environment-variables.go index b8cfff0..e391596 100644 --- a/src/066-environment-variables/environment-variables.go +++ b/src/066-environment-variables/environment-variables.go @@ -2,8 +2,8 @@ package main -// Use the `os` package to list, set, and get -// environment variables. +// Use the `os` package to list, set, and get environment +// variables. import "os" import "strings" import "fmt" diff --git a/src/075-sending-email/sending-email.go b/src/075-sending-email/sending-email.go index 490a86a..8627dad 100644 --- a/src/075-sending-email/sending-email.go +++ b/src/075-sending-email/sending-email.go @@ -27,4 +27,4 @@ func main() { // todo: missing subject, cc, bcc // todo: attachements // todo: plain/multi-type emails -// todo: https://github.com/mtoader/google-go-lang-idea-plugin/issues/112 +// todo: mtoader/google-go-lang-idea-plugin/issues/112 diff --git a/src/082-canonical-hosts/canonical-hosts.go b/src/082-canonical-hosts/canonical-hosts.go index bd9c5ac..e503088 100644 --- a/src/082-canonical-hosts/canonical-hosts.go +++ b/src/082-canonical-hosts/canonical-hosts.go @@ -6,23 +6,27 @@ import "net/http" import "strings" import "fmt" -func hello(res http.ResponseWriter, req *http.Request) { - res.Header().Set("Content-Type", "text/plain") - fmt.Fprintln(res, "Hello canonical world") +type handler http.HandlerFunc + +func hello(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/plain") + fmt.Fprintln(w, "Hello canonical world") } -func wrapCanonicalHost(f http.HandlerFunc, host string) http.HandlerFunc { - return func(res http.ResponseWriter, req *http.Request) { - hostPort := strings.Split(req.Host, ":") +func wrapCanonicalHost(f handler, chost string) handler { + return func(w http.ResponseWriter, r *http.Request) { + hostPort := strings.Split(r.Host, ":") host := hostPort[0] - if host != canonicalHost { - fmt.Println("redirecting from", host, "to", canonicalHost) - hostPort[0] = canonicalHost - url := "http://" + strings.Join(hostPort, ":") + req.URL.String() - http.Redirect(res, req, url, 301) + if host != chost { + fmt.Println("redirect to", chost) + hostPort[0] = chost + url := "http://" + + strings.Join(hostPort, ":") + + r.URL.String() + http.Redirect(w, r, url, 301) return } - f(res, req) + f(w, r) } } diff --git a/src/083-middleware/middleware.go b/src/083-middleware/middleware.go index d7486f0..99dbef2 100644 --- a/src/083-middleware/middleware.go +++ b/src/083-middleware/middleware.go @@ -5,16 +5,15 @@ package main import "net/http" import "fmt" -func hello(res http.ResponseWriter, req *http.Request) { - res.Header().Set("Content-Type", "text/plain") - fmt.Fprintln(res, "Hello wrapped world") +func hello(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/plain") + fmt.Fprintln(w, "Hello wrapped world") } func wrapMiddleware(f http.HandlerFunc) http.HandlerFunc { - return func(res http.ResponseWriter, req *http.Request) { - fmt.Println("before") - f(res, req) - fmt.Println("after") + return func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("X-Wrapped", "true") + f(w, r) } } diff --git a/src/083-middleware/middleware.sh b/src/083-middleware/middleware.sh new file mode 100644 index 0000000..a7bd6b9 --- /dev/null +++ b/src/083-middleware/middleware.sh @@ -0,0 +1,10 @@ +$ go run middleware.go + +$ curl -i http://127.0.0.1:5000/ +HTTP/1.1 200 OK +Content-Type: text/plain +Date: Tue, 02 Oct 2012 00:37:18 GMT +Transfer-Encoding: chunked +X-Wrapped: true + +Hello wrapped world diff --git a/src/084-graceful-shutdown/graceful-shutdown.go b/src/084-graceful-shutdown/graceful-shutdown.go index 5f45849..769148c 100644 --- a/src/084-graceful-shutdown/graceful-shutdown.go +++ b/src/084-graceful-shutdown/graceful-shutdown.go @@ -82,10 +82,12 @@ func main() { for { connCountCurrent := atomic.LoadInt64(&connCount) if connCountCurrent > 0 { - fmt.Println("wait at=pending remaining=", connCountCurrent) + fmt.Println("wait at=pending remaining=", + connCountCurrent) time.Sleep(time.Second) } else { - fmt.Println("wait at=finish remaining=", connCountCurrent) + fmt.Println("wait at=finish remaining=", + connCountCurrent) return } } diff --git a/src/085-https-servers/https-servers.go b/src/085-https-servers/https-servers.go index 4bd253b..f90264e 100644 --- a/src/085-https-servers/https-servers.go +++ b/src/085-https-servers/https-servers.go @@ -11,7 +11,8 @@ func handler(res http.ResponseWriter, req *http.Request) { func main() { http.HandleFunc("/", handler) - err := http.ListenAndServeTLS(":5000", "/tmp/server.crt", "/tmp/server.key", nil) + err := http.ListenAndServeTLS(":5000", + "/tmp/server.crt", "/tmp/server.key", nil) if err != nil { panic(err) } diff --git a/src/085-https-servers/https-servers.sh b/src/085-https-servers/https-servers.sh index 7b63787..5019a74 100644 --- a/src/085-https-servers/https-servers.sh +++ b/src/085-https-servers/https-servers.sh @@ -3,7 +3,8 @@ $ rm -f server.* $ openssl genrsa -des3 -out server.orig.key 2048 $ openssl rsa -in server.orig.key -out server.key $ openssl req -new -key server.key -out server.csr -$ openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt +$ openssl x509 -req -days 365 -in server.csr \ + -signkey server.key -out server.crt $ go run https-servers.go