From 57fd78797fa161125c529d38c01b7b9297fd828b Mon Sep 17 00:00:00 2001 From: Tingfeng Date: Mon, 18 Jul 2022 02:33:12 +0800 Subject: [PATCH] improve readme and demo.go clarity. --- README.md | 9 +++++---- examples/demo.go | 6 ++++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 1b3ab21..f0e77c8 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/examples/demo.go b/examples/demo.go index bfd70a2..e53cb2b 100644 --- a/examples/demo.go +++ b/examples/demo.go @@ -10,6 +10,7 @@ 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{}) @@ -17,7 +18,7 @@ func main() { 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 @@ -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 }