From dfcaebd536ade922c62b780cc23a35155ee7a572 Mon Sep 17 00:00:00 2001 From: Eli Bendersky Date: Fri, 31 May 2019 06:06:13 -0700 Subject: [PATCH 1/5] Basic HTTP client sample --- examples.txt | 1 + examples/http-clients/http-clients.go | 37 ++++++ examples/http-clients/http-clients.hash | 2 + examples/http-clients/http-clients.sh | 7 ++ public/environment-variables | 2 +- public/http-clients | 157 ++++++++++++++++++++++++ public/index.html | 2 + 7 files changed, 207 insertions(+), 1 deletion(-) create mode 100644 examples/http-clients/http-clients.go create mode 100644 examples/http-clients/http-clients.hash create mode 100644 examples/http-clients/http-clients.sh create mode 100644 public/http-clients diff --git a/examples.txt b/examples.txt index ddace2d..9e13b51 100644 --- a/examples.txt +++ b/examples.txt @@ -60,6 +60,7 @@ Line Filters Command-Line Arguments Command-Line Flags Environment Variables +HTTP Clients Spawning Processes Exec'ing Processes Signals diff --git a/examples/http-clients/http-clients.go b/examples/http-clients/http-clients.go new file mode 100644 index 0000000..170f880 --- /dev/null +++ b/examples/http-clients/http-clients.go @@ -0,0 +1,37 @@ +// The Go standard library comes with excellent support +// for HTTP clients and servers out of the box with the +// `net/http` package. In this example we'll use it to +// issue simple HTTP requests. +package main + +import ( + "bufio" + "fmt" + "net/http" +) + +func main() { + // Issue a HTTP GET request to a server. http.Get is a + // convenient shortcut around creating a http.Client + // object and calling its Get() method; it uses the + // http.DefaultClient object which has useful default + // settings. + resp, err := http.Get("http://gobyexample.com") + if err != nil { + panic(err) + } + defer resp.Body.Close() + + // Print the HTTP response status. + fmt.Println("Response status:", resp.Status) + + // Print the first 5 lines of the response body. + scanner := bufio.NewScanner(resp.Body) + for i := 0; scanner.Scan() && i < 5; i++ { + fmt.Println(scanner.Text()) + } + + if err := scanner.Err(); err != nil { + panic(err) + } +} diff --git a/examples/http-clients/http-clients.hash b/examples/http-clients/http-clients.hash new file mode 100644 index 0000000..7addbe9 --- /dev/null +++ b/examples/http-clients/http-clients.hash @@ -0,0 +1,2 @@ +afac2191f24a0861384d142ab58cc262fc47cfbf +Y92yCu1CIrI diff --git a/examples/http-clients/http-clients.sh b/examples/http-clients/http-clients.sh new file mode 100644 index 0000000..afbbb77 --- /dev/null +++ b/examples/http-clients/http-clients.sh @@ -0,0 +1,7 @@ +$ go run http-clients.go +Response status: 200 OK + + + + + Go by Example diff --git a/public/environment-variables b/public/environment-variables index 7e652d1..4d8d276 100644 --- a/public/environment-variables +++ b/public/environment-variables @@ -161,7 +161,7 @@ program picks that value up.

- Next example: Spawning Processes. + Next example: HTTP Clients.

+

Go by Example: HTTP Clients

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+

The Go standard library comes with excellent support +for HTTP clients and servers out of the box with the +net/http package. In this example we’ll use it to +issue simple HTTP requests.

+ +
+ +
package main
+
+ +
+ + + +
import (
+    "bufio"
+    "fmt"
+    "net/http"
+)
+
+ +
+

Issue a HTTP GET request to a server. http.Get is a +convenient shortcut around creating a http.Client +object and calling its Get() method; it uses the +http.DefaultClient object which has useful default +settings.

+ +
+ +
func main() {
+
+ +
+ + + +
    resp, err := http.Get("http://gobyexample.com")
+    if err != nil {
+        panic(err)
+    }
+    defer resp.Body.Close()
+
+ +
+

Print the HTTP response status.

+ +
+ +
    fmt.Println("Response status:", resp.Status)
+
+ +
+

Print the first 5 lines of the response body.

+ +
+ +
    scanner := bufio.NewScanner(resp.Body)
+    for i := 0; scanner.Scan() && i < 5; i++ {
+        fmt.Println(scanner.Text())
+    }
+
+ +
+ + + +
    if err := scanner.Err(); err != nil {
+        panic(err)
+    }
+}
+
+ +
+ + + + + + + + +
+ + + +
$ go run http-clients.go
+Response status: 200 OK
+<!DOCTYPE html>
+<html>
+  <head>
+    <meta charset="utf-8">
+    <title>Go by Example</title>
+
+ +
+ + +

+ Next example: Spawning Processes. +

+ + +
+ + diff --git a/public/index.html b/public/index.html index 92ef12f..6b5727f 100644 --- a/public/index.html +++ b/public/index.html @@ -147,6 +147,8 @@
  • Environment Variables
  • +
  • HTTP Clients
  • +
  • Spawning Processes
  • Exec'ing Processes
  • From d2d9a542087356c98dd23289a0ab391e4ca69332 Mon Sep 17 00:00:00 2001 From: Mark McGranaghan Date: Fri, 31 May 2019 12:10:33 -0700 Subject: [PATCH 2/5] Use fewer words in HTTP Clients intro "out of the box" is implied by "standard library". --- examples/http-clients/http-clients.go | 6 +++--- examples/http-clients/http-clients.hash | 4 ++-- public/http-clients | 8 ++++---- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/examples/http-clients/http-clients.go b/examples/http-clients/http-clients.go index 170f880..6ff5281 100644 --- a/examples/http-clients/http-clients.go +++ b/examples/http-clients/http-clients.go @@ -1,7 +1,7 @@ // The Go standard library comes with excellent support -// for HTTP clients and servers out of the box with the -// `net/http` package. In this example we'll use it to -// issue simple HTTP requests. +// for HTTP clients and servers in the `net/http` +// package. In this example we'll use it to issue simple +// HTTP requests. package main import ( diff --git a/examples/http-clients/http-clients.hash b/examples/http-clients/http-clients.hash index 7addbe9..0e85307 100644 --- a/examples/http-clients/http-clients.hash +++ b/examples/http-clients/http-clients.hash @@ -1,2 +1,2 @@ -afac2191f24a0861384d142ab58cc262fc47cfbf -Y92yCu1CIrI +cdcaa6f2e1e8596e882dd3eb460e633cfe3dd07c +nXc8J7KyUKd diff --git a/public/http-clients b/public/http-clients index c1dadf0..9bf3077 100644 --- a/public/http-clients +++ b/public/http-clients @@ -14,13 +14,13 @@

    The Go standard library comes with excellent support -for HTTP clients and servers out of the box with the -net/http package. In this example we’ll use it to -issue simple HTTP requests.

    +for HTTP clients and servers in the net/http +package. In this example we’ll use it to issue simple +HTTP requests.

    - +
    package main
     
    From 60bed1aa0d666bf4cb7bf82427f552fa8ab967f0 Mon Sep 17 00:00:00 2001 From: Mark McGranaghan Date: Fri, 31 May 2019 12:11:24 -0700 Subject: [PATCH 3/5] Update whitespace so comment aligns with relevant code --- examples/http-clients/http-clients.go | 1 + examples/http-clients/http-clients.hash | 4 ++-- public/http-clients | 16 ++++++++-------- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/examples/http-clients/http-clients.go b/examples/http-clients/http-clients.go index 6ff5281..e986748 100644 --- a/examples/http-clients/http-clients.go +++ b/examples/http-clients/http-clients.go @@ -11,6 +11,7 @@ import ( ) func main() { + // Issue a HTTP GET request to a server. http.Get is a // convenient shortcut around creating a http.Client // object and calling its Get() method; it uses the diff --git a/examples/http-clients/http-clients.hash b/examples/http-clients/http-clients.hash index 0e85307..bd99392 100644 --- a/examples/http-clients/http-clients.hash +++ b/examples/http-clients/http-clients.hash @@ -1,2 +1,2 @@ -cdcaa6f2e1e8596e882dd3eb460e633cfe3dd07c -nXc8J7KyUKd +991df79b50b0d512a034ec929339a06a1e6230ee +lz5Z7oVYqyF diff --git a/public/http-clients b/public/http-clients index 9bf3077..76419d8 100644 --- a/public/http-clients +++ b/public/http-clients @@ -20,7 +20,7 @@ HTTP requests.

    - +
    package main
     
    @@ -45,12 +45,7 @@ HTTP requests.

    -

    Issue a HTTP GET request to a server. http.Get is a -convenient shortcut around creating a http.Client -object and calling its Get() method; it uses the -http.DefaultClient object which has useful default -settings.

    - + @@ -62,7 +57,12 @@ settings.

    - +

    Issue a HTTP GET request to a server. http.Get is a +convenient shortcut around creating a http.Client +object and calling its Get() method; it uses the +http.DefaultClient object which has useful default +settings.

    + From c209145948f604fe8a2751efee837a78ed5c1f43 Mon Sep 17 00:00:00 2001 From: Mark McGranaghan Date: Fri, 31 May 2019 12:14:54 -0700 Subject: [PATCH 4/5] "An HTTP" This could go either way, but my pronunciation of HTTP dictates "an" (: https://www.quora.com/A-http-or-an-http-which-is-the-correct-grammatical-usage --- examples/http-clients/http-clients.go | 4 ++-- examples/http-clients/http-clients.hash | 4 ++-- public/http-clients | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/examples/http-clients/http-clients.go b/examples/http-clients/http-clients.go index e986748..b1e6db2 100644 --- a/examples/http-clients/http-clients.go +++ b/examples/http-clients/http-clients.go @@ -12,8 +12,8 @@ import ( func main() { - // Issue a HTTP GET request to a server. http.Get is a - // convenient shortcut around creating a http.Client + // Issue an HTTP GET request to a server. http.Get is a + // convenient shortcut around creating an http.Client // object and calling its Get() method; it uses the // http.DefaultClient object which has useful default // settings. diff --git a/examples/http-clients/http-clients.hash b/examples/http-clients/http-clients.hash index bd99392..3057f1d 100644 --- a/examples/http-clients/http-clients.hash +++ b/examples/http-clients/http-clients.hash @@ -1,2 +1,2 @@ -991df79b50b0d512a034ec929339a06a1e6230ee -lz5Z7oVYqyF +84f4d5d6264837f07a84c27a09f14e429e8daf94 +HDsvgdX4tGg diff --git a/public/http-clients b/public/http-clients index 76419d8..5e482e4 100644 --- a/public/http-clients +++ b/public/http-clients @@ -20,7 +20,7 @@ HTTP requests.

    - +
    package main
     
    @@ -57,8 +57,8 @@ HTTP requests.

    -

    Issue a HTTP GET request to a server. http.Get is a -convenient shortcut around creating a http.Client +

    Issue an HTTP GET request to a server. http.Get is a +convenient shortcut around creating an http.Client object and calling its Get() method; it uses the http.DefaultClient object which has useful default settings.

    From 41d6630bdd716f4c97a93e676732374245899b8c Mon Sep 17 00:00:00 2001 From: Mark McGranaghan Date: Fri, 31 May 2019 12:18:58 -0700 Subject: [PATCH 5/5] Normalize code fragment typography Per convention, code fragments get monotype and methods in the text don't get parens or parameters. --- examples/http-clients/http-clients.go | 8 ++++---- examples/http-clients/http-clients.hash | 4 ++-- public/http-clients | 10 +++++----- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/examples/http-clients/http-clients.go b/examples/http-clients/http-clients.go index b1e6db2..56be133 100644 --- a/examples/http-clients/http-clients.go +++ b/examples/http-clients/http-clients.go @@ -12,10 +12,10 @@ import ( func main() { - // Issue an HTTP GET request to a server. http.Get is a - // convenient shortcut around creating an http.Client - // object and calling its Get() method; it uses the - // http.DefaultClient object which has useful default + // Issue an HTTP GET request to a server. `http.Get` is a + // convenient shortcut around creating an `http.Client` + // object and calling its `Get` method; it uses the + // `http.DefaultClient` object which has useful default // settings. resp, err := http.Get("http://gobyexample.com") if err != nil { diff --git a/examples/http-clients/http-clients.hash b/examples/http-clients/http-clients.hash index 3057f1d..e550dd5 100644 --- a/examples/http-clients/http-clients.hash +++ b/examples/http-clients/http-clients.hash @@ -1,2 +1,2 @@ -84f4d5d6264837f07a84c27a09f14e429e8daf94 -HDsvgdX4tGg +ec8fd69aa19e54a7ea05d2a911f09d3a98f0396c +VxYIifr_UuH diff --git a/public/http-clients b/public/http-clients index 5e482e4..21779dd 100644 --- a/public/http-clients +++ b/public/http-clients @@ -20,7 +20,7 @@ HTTP requests.

    - +
    package main
     
    @@ -57,10 +57,10 @@ HTTP requests.

    -

    Issue an HTTP GET request to a server. http.Get is a -convenient shortcut around creating an http.Client -object and calling its Get() method; it uses the -http.DefaultClient object which has useful default +

    Issue an HTTP GET request to a server. http.Get is a +convenient shortcut around creating an http.Client +object and calling its Get method; it uses the +http.DefaultClient object which has useful default settings.