Go contains rich function for grab web contents. net/http is the major library. Ref: golang.org. |
package main
|
import (
"net/http"
"net/url"
)
import "io/ioutil"
import "fmt"
import "strings"
|
|
keep first n lines |
func keepLines(s string, n int) string {
result := strings.Join(strings.Split(s, "\n")[:n], "\n")
return strings.Replace(result, "\r", "", -1)
}
|
We can use GET form to get result. |
func main() {
|
resp, err := http.Get("http://g.cn/robots.txt")
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
fmt.Println("get:\n", keepLines(string(body), 3))
|
|
We can use POST form to get result, too. |
resp, err = http.PostForm("http://duckduckgo.com",
url.Values{"q": {"github"}})
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err = ioutil.ReadAll(resp.Body)
fmt.Println("post:\n", keepLines(string(body), 3))
}
|
$ go run http-client.go
get:
User-agent: *
Disallow: /search
Disallow: /sdch
post:
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
|
Previous example: Exit.
Next example: Text Template.