From 0de3674882012891b408bed8be944dbe9b3c5152 Mon Sep 17 00:00:00 2001 From: Mark McGranaghan Date: Sun, 23 Sep 2012 12:07:35 -0700 Subject: [PATCH] updates --- .../069-http-server-static.go | 21 ------------ 069-http-server-static/http-server-static.go | 14 ++++++++ 069-http-server-static/http-server-static.sh | 5 +++ ...tus-code.go => http-server-status-code.go} | 11 +++--- .../http-server-status-code.sh | 5 +++ .../{071-http-server.go => http-server.go} | 2 ++ .../{072-https-client.go => https-client.go} | 7 +++- 073-https-server/073-https-server.go | 26 -------------- 073-https-server/https-server.go | 16 +++++++++ 073-https-server/https-server.sh | 11 ++++++ 074-json/{074-json.go => json.go} | 5 ++- 075-number-parsing/075-number-parsing.go | 34 ------------------- 075-number-parsing/number-parsing.go | 30 ++++++++++++++++ 075-number-parsing/number-parsing.sh | 6 ++++ 14 files changed, 103 insertions(+), 90 deletions(-) delete mode 100644 069-http-server-static/069-http-server-static.go create mode 100644 069-http-server-static/http-server-static.go create mode 100644 069-http-server-static/http-server-static.sh rename 070-http-server-status-code/{070-http-server-status-code.go => http-server-status-code.go} (67%) create mode 100644 070-http-server-status-code/http-server-status-code.sh rename 071-http-server/{071-http-server.go => http-server.go} (93%) rename 072-https-client/{072-https-client.go => https-client.go} (78%) delete mode 100644 073-https-server/073-https-server.go create mode 100644 073-https-server/https-server.go create mode 100644 073-https-server/https-server.sh rename 074-json/{074-json.go => json.go} (94%) delete mode 100644 075-number-parsing/075-number-parsing.go create mode 100644 075-number-parsing/number-parsing.go create mode 100644 075-number-parsing/number-parsing.sh diff --git a/069-http-server-static/069-http-server-static.go b/069-http-server-static/069-http-server-static.go deleted file mode 100644 index eb2c843..0000000 --- a/069-http-server-static/069-http-server-static.go +++ /dev/null @@ -1,21 +0,0 @@ -package main - -import "net/http" - -func main() { - handler := http.FileServer(http.Dir("./")) - http.ListenAndServe(":5000", handler) -} - -// == running -// $ cd src -// $ go run xx-http-server-static.go -// -// $ curl http://127.0.0.1:5000/ -// $ curl http://127.0.0.1:5000/xx-http-server-static.go -// $ curl http://127.0.0.1:5000/missing - -// == todo -// index pages -// disable listing -// custom 404 handling diff --git a/069-http-server-static/http-server-static.go b/069-http-server-static/http-server-static.go new file mode 100644 index 0000000..0f0acbf --- /dev/null +++ b/069-http-server-static/http-server-static.go @@ -0,0 +1,14 @@ +// ## HTTP Server Static + +package main + +import "net/http" + +func main() { + handler := http.FileServer(http.Dir("./")) + http.ListenAndServe(":5000", handler) +} + +// todo: index pages +// todo: disable listing +// todo: custom 404 handling diff --git a/069-http-server-static/http-server-static.sh b/069-http-server-static/http-server-static.sh new file mode 100644 index 0000000..bd95207 --- /dev/null +++ b/069-http-server-static/http-server-static.sh @@ -0,0 +1,5 @@ +$ go run http-server-static.go + +$ curl http://127.0.0.1:5000/ +$ curl http://127.0.0.1:5000/http-server-static.go +$ curl http://127.0.0.1:5000/missing diff --git a/070-http-server-status-code/070-http-server-status-code.go b/070-http-server-status-code/http-server-status-code.go similarity index 67% rename from 070-http-server-status-code/070-http-server-status-code.go rename to 070-http-server-status-code/http-server-status-code.go index 230c8de..b51748e 100644 --- a/070-http-server-status-code/070-http-server-status-code.go +++ b/070-http-server-status-code/http-server-status-code.go @@ -1,6 +1,9 @@ +// ## HTTP Server Status Code + package main -import ("net/http"; "fmt") +import "net/http" +import "fmt" func hello(res http.ResponseWriter, req *http.Request) { res.Header().Set("Content-Type", "text/plain") @@ -16,9 +19,3 @@ func main() { http.HandleFunc("/", hello) http.ListenAndServe(":5000", nil) } - -// running -// $ go run xx-http-server-status-code.go -// -// $ curl -i http://127.0.0.1:5000/ -// $ curl -i http://127.0.0.1:5000/protected diff --git a/070-http-server-status-code/http-server-status-code.sh b/070-http-server-status-code/http-server-status-code.sh new file mode 100644 index 0000000..ebae391 --- /dev/null +++ b/070-http-server-status-code/http-server-status-code.sh @@ -0,0 +1,5 @@ +$ go run xx-http-server-status-code.go + +$ curl -i http://127.0.0.1:5000/ | grep HTTP + +$ curl -i http://127.0.0.1:5000/protected | grep HTTP diff --git a/071-http-server/071-http-server.go b/071-http-server/http-server.go similarity index 93% rename from 071-http-server/071-http-server.go rename to 071-http-server/http-server.go index adf501e..8f61694 100644 --- a/071-http-server/071-http-server.go +++ b/071-http-server/http-server.go @@ -1,3 +1,5 @@ +// ## HTTP Server + package main import "net/http" diff --git a/072-https-client/072-https-client.go b/072-https-client/https-client.go similarity index 78% rename from 072-https-client/072-https-client.go rename to 072-https-client/https-client.go index 587368d..f5c8d08 100644 --- a/072-https-client/072-https-client.go +++ b/072-https-client/https-client.go @@ -1,6 +1,11 @@ +// ## HTTPS Client + package main -import ("net/http"; "crypto/tls"; "io/ioutil"; "fmt") +import "net/http" +import "crypto/tls" +import "io/ioutil" +import "fmt" func main() { tr := &http.Transport{ diff --git a/073-https-server/073-https-server.go b/073-https-server/073-https-server.go deleted file mode 100644 index 8416f05..0000000 --- a/073-https-server/073-https-server.go +++ /dev/null @@ -1,26 +0,0 @@ -package main - -import "net/http" - -func handler(res http.ResponseWriter, req *http.Request) { - res.Header().Set("Content-Type", "text/plain") - res.Write([]byte("Hello from HTTPS\n")) -} - -func main() { - http.HandleFunc("/", handler) - err := http.ListenAndServeTLS(":5000", "/tmp/server.crt", "/tmp/server.key", nil) - if err != nil { panic(err) } -} - -// $ cd /tmp -// $ 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 - -// $ go run src/xx-https-server.go - -// $ curl https://127.0.0.1:5000/ -// $ curl --insecure https://127.0.0.1:5000/ diff --git a/073-https-server/https-server.go b/073-https-server/https-server.go new file mode 100644 index 0000000..ecbdc17 --- /dev/null +++ b/073-https-server/https-server.go @@ -0,0 +1,16 @@ +// ## HTTPS Server + +package main + +import "net/http" + +func handler(res http.ResponseWriter, req *http.Request) { + res.Header().Set("Content-Type", "text/plain") + res.Write([]byte("Hello from HTTPS\n")) +} + +func main() { + http.HandleFunc("/", handler) + err := http.ListenAndServeTLS(":5000", "/tmp/server.crt", "/tmp/server.key", nil) + if err != nil { panic(err) } +} diff --git a/073-https-server/https-server.sh b/073-https-server/https-server.sh new file mode 100644 index 0000000..8e03639 --- /dev/null +++ b/073-https-server/https-server.sh @@ -0,0 +1,11 @@ +$ cd /tmp +$ 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 + +$ go run src/xx-https-server.go + +$ curl https://127.0.0.1:5000/ +$ curl --insecure https://127.0.0.1:5000/ diff --git a/074-json/074-json.go b/074-json/json.go similarity index 94% rename from 074-json/074-json.go rename to 074-json/json.go index c8f3c16..927aaf9 100644 --- a/074-json/074-json.go +++ b/074-json/json.go @@ -1,6 +1,9 @@ +// JSON + package main -import ("encoding/json"; "fmt") +import "encoding/json" +import "fmt" func main() { // data to bytes/string diff --git a/075-number-parsing/075-number-parsing.go b/075-number-parsing/075-number-parsing.go deleted file mode 100644 index eda49b0..0000000 --- a/075-number-parsing/075-number-parsing.go +++ /dev/null @@ -1,34 +0,0 @@ - // Number parsing - -package main - -import ( - "fmt" - "strconv" // Package `strconv` provides the parsing. -) - -func main() { - f, _ := strconv.ParseFloat("1.234", 64) // `64` tells how many bits of precision to parse. - fmt.Println(f) - - i, _ := strconv.ParseInt("123", 0, 64) // `0` means infer the base from the string. - println(i) // `64` requires that the result fit in 64 bits. - - d, _ := strconv.ParseInt("0x1b3e", 0, 64) // `ParseInt` will recognize hex-formatted numbers. - println(d) - - k, _ := strconv.Atoi("456") // `Atoi` is a convenienice function for `int` parsing. - println(k) - - _, e := strconv.Atoi("wat") // Parse functions return an error on bad input. - fmt.Println(e) -} - -/* -$ go run xx-number-parsing.go -1.234 -123 -6974 -456 -strconv.ParseInt: parsing "wat": invalid syntax -*/ diff --git a/075-number-parsing/number-parsing.go b/075-number-parsing/number-parsing.go new file mode 100644 index 0000000..de07b4a --- /dev/null +++ b/075-number-parsing/number-parsing.go @@ -0,0 +1,30 @@ +// ## Number Parsing + +package main + +// Package `strconv` provides the number parsing. +import "strconv" +import "fmt" + +func main() { + // `64` tells how many bits of precision to parse. + f, _ := strconv.ParseFloat("1.234", 64) + fmt.Println(f) + + // `0` means infer the base from the string. + // `64` requires that the result fit in 64 bits. + i, _ := strconv.ParseInt("123", 0, 64) + println(i) + + // `ParseInt` will recognize hex-formatted numbers. + d, _ := strconv.ParseInt("0x1b3e", 0, 64) + println(d) + + // `Atoi` is a convenienice function for `int` parsing. + k, _ := strconv.Atoi("456") + println(k) + + // Parse functions return an error on bad input. + _, e := strconv.Atoi("wat") + fmt.Println(e) +} diff --git a/075-number-parsing/number-parsing.sh b/075-number-parsing/number-parsing.sh new file mode 100644 index 0000000..ed5819d --- /dev/null +++ b/075-number-parsing/number-parsing.sh @@ -0,0 +1,6 @@ +$ go run xx-number-parsing.go +1.234 +123 +6974 +456 +strconv.ParseInt: parsing "wat": invalid syntax