Golang : How to validate ISBN?
This tutorial will show you an example on how to validate ISBN with the govalidator package and then get the book information from http://www.isbnsearch.org/
. The HTML data dump will not be parsed, but you can extract the relevant information you want easily. See this tutorial on how to extract links from web page to get the idea on how to extract the book title and prices.
NOTE : There are two versions of ISBN(International Standard Book Number) standard. The ISBN is 13 digits long if assigned on or after 1 January 2007, and 10 digits long if assigned before 2007.
Here you go!
package main
import (
"fmt"
"github.com/asaskevich/govalidator"
"io/ioutil"
"net/http"
"os"
"strings"
)
func main() {
isbn := "0-201-53377-4" // before 2007 - 10 digits
// see https://en.wikipedia.org/wiki/International_Standard_Book_Number
validISBN := govalidator.IsISBN(isbn, 10) //use version 10
// can use govalidator.IsISBN10 or IsISBN13 as well.
fmt.Printf("%s is a valid ISBN : %v \n", isbn, validISBN)
if validISBN {
// remove all dashes
noDashISBN := strings.Replace(isbn, "-", "", -1)
resp, err := http.Get("http://www.isbnsearch.org/isbn/" + noDashISBN)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer resp.Body.Close()
htmlData, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(os.Stdout, string(htmlData))
// see https://www.socketloop.com/tutorials/golang-how-to-extract-links-from-web-page
// on how to scrape the information you want from HTML
}
}
Happy coding!
References :
https://en.wikipedia.org/wiki/InternationalStandardBook_Number
See also : Golang : How to extract links from web page ?
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+7k Golang : How to iterate a slice without using for loop?
+16.1k Golang : Test floating point numbers not-a-number and infinite example
+40.7k Golang : How to check if a string contains another sub-string?
+22.7k Golang : Gorilla mux routing example
+8.8k Golang : How to use Gorilla webtoolkit context package properly
+25.6k Golang : Convert IP address string to long ( unsigned 32-bit integer )
+8.8k Golang : Capture text return from exec function example
+10.8k Golang : Simple image viewer with Go-GTK
+23.8k Golang : Upload to S3 with official aws-sdk-go package
+18.3k Golang : Find IP address from string
+10.9k Golang : Proper way to test CIDR membership of an IP 4 or 6 address example
+9.7k Golang : Check if user agent is a robot or crawler example