package main
import (
"fmt"
"strconv"
)
func sum10(isbn string) (int, error) {
s := 0
w := 10
for k, v := range isbn {
if k == 9 && v == 88 {
s += 10
} else {
n, err := strconv.Atoi(string(v))
if err != nil {
return -1, fmt.Errorf("Failed to convert ISBN-10 character to int: %s", string(v))
}
s += n * w
}
w--
}
return s, nil
}
func validISBN10(isbn10 string) (string, error) {
if len(isbn10) != 9 && len(isbn10) != 10 {
return "", fmt.Errorf("A string of length 9 or 10 is required to calculate the ISBN-10 check digit. Provided was: %s", isbn10)
}
s, err := sum10(isbn10[:9])
if err != nil {
return "", err
}
d := (11 - (s % 11)) % 11
if d == 10 {
return "X", nil
}
return strconv.Itoa(d), nil
}
func main() {
str := "1112223339"
_, err := validISBN10(str)
if err != nil {
fmt.Println("InValid")
} else {
fmt.Println("Valid")
}
}
Valid