Skip to content

Commit

Permalink
Merge pull request #17 from c4s4/master
Browse files Browse the repository at this point in the history
Added syntax colorization in sample codes
mna committed Sep 3, 2015

Verified

This commit was signed with the committer’s verified signature.
m8rmclaren Hayden
2 parents 4587cf0 + bad2093 commit 8f23e32
Showing 1 changed file with 34 additions and 30 deletions.
64 changes: 34 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
@@ -31,29 +31,31 @@ The following example (taken from /example/short/main.go) shows how to create an
start a Fetcher, one way to send commands, and how to stop the fetcher once all
commands have been handled.

package main

import (
"fmt"
"net/http"

"github.com/PuerkitoBio/fetchbot"
)

func main() {
f := fetchbot.New(fetchbot.HandlerFunc(handler))
queue := f.Start()
queue.SendStringHead("http://google.com", "http://golang.org", "http://golang.org/doc")
queue.Close()
}

func handler(ctx *fetchbot.Context, res *http.Response, err error) {
if err != nil {
fmt.Printf("error: %s\n", err)
return
}
fmt.Printf("[%d] %s %s\n", res.StatusCode, ctx.Cmd.Method(), ctx.Cmd.URL())
}
```go
package main

import (
"fmt"
"net/http"

"github.com/PuerkitoBio/fetchbot"
)

func main() {
f := fetchbot.New(fetchbot.HandlerFunc(handler))
queue := f.Start()
queue.SendStringHead("http://google.com", "http://golang.org", "http://golang.org/doc")
queue.Close()
}

func handler(ctx *fetchbot.Context, res *http.Response, err error) {
if err != nil {
fmt.Printf("error: %s\n", err)
return
}
fmt.Printf("[%d] %s %s\n", res.StatusCode, ctx.Cmd.Method(), ctx.Cmd.URL())
}
```

A more complex and complete example can be found in the repository, at /example/full/.

@@ -70,13 +72,15 @@ thread-safe object that can be used to send commands, or to stop the crawler.
Both the Command and the Handler are interfaces, and may be implemented in various ways.
They are defined like so:

type Command interface {
URL() *url.URL
Method() string
}
type Handler interface {
Handle(*Context, *http.Response, error)
}
```go
type Command interface {
URL() *url.URL
Method() string
}
type Handler interface {
Handle(*Context, *http.Response, error)
}
```

A **Context** is a struct that holds the Command and the Queue, so that the Handler always
knows which Command initiated this call, and has a handle to the Queue.

0 comments on commit 8f23e32

Please sign in to comment.