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..56be133 --- /dev/null +++ b/examples/http-clients/http-clients.go @@ -0,0 +1,38 @@ +// The Go standard library comes with excellent support +// 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 ( + "bufio" + "fmt" + "net/http" +) + +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 + // 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..e550dd5 --- /dev/null +++ b/examples/http-clients/http-clients.hash @@ -0,0 +1,2 @@ +ec8fd69aa19e54a7ea05d2a911f09d3a98f0396c +VxYIifr_UuH 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 in 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 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 {
+        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