Merge branch 'httpclient-sample'
This commit is contained in:
commit
fe60e81e4e
@ -60,6 +60,7 @@ Line Filters
|
||||
Command-Line Arguments
|
||||
Command-Line Flags
|
||||
Environment Variables
|
||||
HTTP Clients
|
||||
Spawning Processes
|
||||
Exec'ing Processes
|
||||
Signals
|
||||
|
38
examples/http-clients/http-clients.go
Normal file
38
examples/http-clients/http-clients.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
2
examples/http-clients/http-clients.hash
Normal file
2
examples/http-clients/http-clients.hash
Normal file
@ -0,0 +1,2 @@
|
||||
ec8fd69aa19e54a7ea05d2a911f09d3a98f0396c
|
||||
VxYIifr_UuH
|
7
examples/http-clients/http-clients.sh
Normal file
7
examples/http-clients/http-clients.sh
Normal file
@ -0,0 +1,7 @@
|
||||
$ go run http-clients.go
|
||||
Response status: 200 OK
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Go by Example</title>
|
@ -161,7 +161,7 @@ program picks that value up.</p>
|
||||
|
||||
|
||||
<p class="next">
|
||||
Next example: <a href="spawning-processes">Spawning Processes</a>.
|
||||
Next example: <a href="http-clients">HTTP Clients</a>.
|
||||
</p>
|
||||
|
||||
<p class="footer">
|
||||
|
157
public/http-clients
Normal file
157
public/http-clients
Normal file
@ -0,0 +1,157 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Go by Example: HTTP Clients</title>
|
||||
<link rel=stylesheet href="site.css">
|
||||
</head>
|
||||
<body>
|
||||
<div class="example" id="http-clients">
|
||||
<h2><a href="./">Go by Example</a>: HTTP Clients</h2>
|
||||
|
||||
<table>
|
||||
|
||||
<tr>
|
||||
<td class="docs">
|
||||
<p>The Go standard library comes with excellent support
|
||||
for HTTP clients and servers in the <code>net/http</code>
|
||||
package. In this example we’ll use it to issue simple
|
||||
HTTP requests.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
<a href="http://play.golang.org/p/VxYIifr_UuH"><img title="Run code" class="run" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAPCAYAAADtc08vAAAABGdBTUEAANbY1E9YMgAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAJ1SURBVCjPY/j//z8DMu7o6GAAgpQgD9tLqcmJH4KDg14aaik/MtdXe2ZjY6OCrh6Fs2jRYmZ9Pd05M9uL/u9dPfU/CLS0dfxvKIz/X5Dg/z8pKdkGqwGpqakMUdExDHJSYqt37tjxf+qUSf9rc2P+79298/+RA3v+H1zV///o6r7/DrbWFQkJiQwxMTGoBjAxMTpKiQmuqMuP/f/xw/v/J0+f/W9tbvTfxVLn/8rJVf+v757z/96hRf8TQtxuCQmLMjk4OKAawMfDVWVvrvd85eTq/7tXTP6/e/XM/22lif9LCnL+b13Q/v/Kzln/L++c/X/7/Jb/VpYWuZFRUagGAAErEBtlxvi+vn944f9L26cDNcz6v21R9/8zm6aC2SBDbu+f/78kK+4/L79AO7oBYCAqxD/57JZp/y/tmPX/wrYZ/6+CbAayD6zs/78daBjIgPayFJAGG6wGAIFAcpjH/dv7F4ANABuya/b/Od3l/ye2V/+/tnv2/7ldxSANmrgMYGBhZg7fuagD7GyYIeeBrrqwdRrQgLn/l02sBGkwwWkAEAjV5EZ/vQV0LswAGAYZsLC3DKTBAJ8BzCkRni/uHFyIYcAtoNc6ypL/ANVIohigrKwMxqqqqgxMzKzM6VHeL+6iGQAKzDtAV5XlJv3n5uFLRTHgzZs3YPzz50+GwqJiPitD9Y8Pjy4BB+CNvfP+3wUmIpAhhckhr3X19LodHZ28UQxQU1MDYw0NDQYBAQEeoBOTK7JjP2xf3Pt/bkfB/4KkoDcKMmIL5OXlFerq6hhu3rzJgC8MwMDYxGSfm5vbVn9/f0cgVxAkpqioyFBfX49iAACbTAK+xT3CzgAAAABJRU5ErkJggg==" /></a>
|
||||
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td class="docs">
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="p">(</span>
|
||||
<span class="s">"bufio"</span>
|
||||
<span class="s">"fmt"</span>
|
||||
<span class="s">"net/http"</span>
|
||||
<span class="p">)</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td class="docs">
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre><span class="kd">func</span> <span class="nx">main</span><span class="p">()</span> <span class="p">{</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td class="docs">
|
||||
<p>Issue an HTTP GET request to a server. <code>http.Get</code> is a
|
||||
convenient shortcut around creating an <code>http.Client</code>
|
||||
object and calling its <code>Get</code> method; it uses the
|
||||
<code>http.DefaultClient</code> object which has useful default
|
||||
settings.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre> <span class="nx">resp</span><span class="p">,</span> <span class="nx">err</span> <span class="o">:=</span> <span class="nx">http</span><span class="p">.</span><span class="nx">Get</span><span class="p">(</span><span class="s">"http://gobyexample.com"</span><span class="p">)</span>
|
||||
<span class="k">if</span> <span class="nx">err</span> <span class="o">!=</span> <span class="kc">nil</span> <span class="p">{</span>
|
||||
<span class="nb">panic</span><span class="p">(</span><span class="nx">err</span><span class="p">)</span>
|
||||
<span class="p">}</span>
|
||||
<span class="k">defer</span> <span class="nx">resp</span><span class="p">.</span><span class="nx">Body</span><span class="p">.</span><span class="nx">Close</span><span class="p">()</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td class="docs">
|
||||
<p>Print the HTTP response status.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre> <span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="s">"Response status:"</span><span class="p">,</span> <span class="nx">resp</span><span class="p">.</span><span class="nx">Status</span><span class="p">)</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td class="docs">
|
||||
<p>Print the first 5 lines of the response body.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre> <span class="nx">scanner</span> <span class="o">:=</span> <span class="nx">bufio</span><span class="p">.</span><span class="nx">NewScanner</span><span class="p">(</span><span class="nx">resp</span><span class="p">.</span><span class="nx">Body</span><span class="p">)</span>
|
||||
<span class="k">for</span> <span class="nx">i</span> <span class="o">:=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">scanner</span><span class="p">.</span><span class="nx">Scan</span><span class="p">()</span> <span class="o">&&</span> <span class="nx">i</span> <span class="p"><</span> <span class="mi">5</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span> <span class="p">{</span>
|
||||
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="nx">scanner</span><span class="p">.</span><span class="nx">Text</span><span class="p">())</span>
|
||||
<span class="p">}</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td class="docs">
|
||||
|
||||
</td>
|
||||
<td class="code">
|
||||
|
||||
<div class="highlight"><pre> <span class="k">if</span> <span class="nx">err</span> <span class="o">:=</span> <span class="nx">scanner</span><span class="p">.</span><span class="nx">Err</span><span class="p">();</span> <span class="nx">err</span> <span class="o">!=</span> <span class="kc">nil</span> <span class="p">{</span>
|
||||
<span class="nb">panic</span><span class="p">(</span><span class="nx">err</span><span class="p">)</span>
|
||||
<span class="p">}</span>
|
||||
<span class="p">}</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
</table>
|
||||
|
||||
<table>
|
||||
|
||||
<tr>
|
||||
<td class="docs">
|
||||
|
||||
</td>
|
||||
<td class="code">
|
||||
|
||||
<div class="highlight"><pre><span class="gp">$</span> go run http-clients.go
|
||||
<span class="go">Response status: 200 OK</span>
|
||||
<span class="go"><!DOCTYPE html></span>
|
||||
<span class="go"><html></span>
|
||||
<span class="go"> <head></span>
|
||||
<span class="go"> <meta charset="utf-8"></span>
|
||||
<span class="go"> <title>Go by Example</title></span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
</table>
|
||||
|
||||
|
||||
<p class="next">
|
||||
Next example: <a href="spawning-processes">Spawning Processes</a>.
|
||||
</p>
|
||||
|
||||
<p class="footer">
|
||||
by <a href="https://markmcgranaghan.com">Mark McGranaghan</a> | <a href="https://github.com/mmcgrana/gobyexample/blob/master/examples/http-clients">source</a> | <a href="https://github.com/mmcgrana/gobyexample#license">license</a>
|
||||
</p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
@ -147,6 +147,8 @@
|
||||
|
||||
<li><a href="environment-variables">Environment Variables</a></li>
|
||||
|
||||
<li><a href="http-clients">HTTP Clients</a></li>
|
||||
|
||||
<li><a href="spawning-processes">Spawning Processes</a></li>
|
||||
|
||||
<li><a href="execing-processes">Exec'ing Processes</a></li>
|
||||
|
Loading…
x
Reference in New Issue
Block a user