Skip to content

Commit

Permalink
improve readme and demo.go clarity.
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotwutingfeng committed Jul 17, 2022
1 parent 37e7cc0 commit 57fd787
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ Afterwards, try extracting subcomponents from a URL.

## Try the example code

All of the following examples can be found at `examples/demo.go`. To run the demo, use the following command:
All of the following examples can be found at `examples/demo.go`. To play the demo, run the following command:

```sh
# `git clone` and `cd` to the go-fasttld repository folder first
make demo
```

### Domain
### Hostname

```go
// Initialise fasttld extractor
Expand Down Expand Up @@ -206,16 +206,17 @@ res, _ := extractor.Extract(fasttld.URLParams{URL: url, ConvertURLToPunyCode: tr

### Parsing errors

`Extract()` returns an error if the parser detects the URL as invalid.
If the URL is invalid, the second variable returned by `Extract()`, **error**, will be non-nil. Partially extracted subcomponents can still be retrieved from the first variable returned, ***ExtractResult**.

```go
extractor, _ := fasttld.New(fasttld.SuffixListParams{})
url := "https://example!.com" // invalid characters in hostname
color.New().Println("The following line should be an error message")
if _, err := extractor.Extract(fasttld.URLParams{URL: url}); err != nil {
if res, err := extractor.Extract(fasttld.URLParams{URL: url}); err != nil {
color.New(color.FgHiRed, color.Bold).Print("Error: ")
color.New(color.FgHiWhite).Println(err)
}
fasttld.PrintRes(url, res) // Partially extracted subcomponents can still be retrieved
```

## Testing
Expand Down
6 changes: 4 additions & 2 deletions examples/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import (
func main() {
var fontStyle = []color.Attribute{color.FgHiWhite, color.Bold}

// Hostname
url := "https://user@a.subdomain.example.ac.uk:5000/a/b?id=42"

extractor, err := fasttld.New(fasttld.SuffixListParams{})
if err != nil {
log.Fatal(err)
}
res, _ := extractor.Extract(fasttld.URLParams{URL: url})
color.New(fontStyle...).Println("Domain")
color.New(fontStyle...).Println("Hostname")
fasttld.PrintRes(url, res)

// Specify custom public suffix list file
Expand Down Expand Up @@ -87,8 +88,9 @@ func main() {

color.New(fontStyle...).Println("Parsing errors")
color.New().Println("The following line should be an error message")
if _, err := extractor.Extract(fasttld.URLParams{URL: url}); err != nil {
if res, err = extractor.Extract(fasttld.URLParams{URL: url}); err != nil {
color.New(color.FgHiRed, color.Bold).Print("Error: ")
color.New(color.FgHiWhite).Println(err)
}
fasttld.PrintRes(url, res) // Partially extracted subcomponents can still be retrieved
}

0 comments on commit 57fd787

Please sign in to comment.